Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

newforms-admin: Added link to current file for file-based widgets in …

…the admin

git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@6457 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit cc1d4c06c53c703eb77b3a9e9ed2c55ca2017cec 1 parent a83d1e9
Christian Metts mintchaos authored
5 django/contrib/admin/options.py
View
@@ -164,6 +164,11 @@ def formfield_for_dbfield(self, db_field, **kwargs):
kwargs['widget'] = widgets.AdminTimeWidget
return db_field.formfield(**kwargs)
+ # For FileFields and ImageFields add a link to the current file.
+ if isinstance(db_field, models.ImageField) or isinstance(db_field, models.FileField):
+ kwargs['widget'] = widgets.AdminFileWidget
+ return db_field.formfield(**kwargs)
+
# For ForeignKey or ManyToManyFields, use a special widget.
if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)):
if isinstance(db_field, models.ForeignKey) and db_field.name in self.raw_id_fields:
15 django/contrib/admin/widgets.py
View
@@ -60,6 +60,21 @@ def format_output(self, rendered_widgets):
return u'<p class="datetime">%s %s<br />%s %s</p>' % \
(_('Date:'), rendered_widgets[0], _('Time:'), rendered_widgets[1])
+class AdminFileWidget(forms.FileInput):
+ """
+ A FileField Widget that shows it's current value if it has one
+ """
+ def __init__(self, attrs={}):
+ super(AdminFileWidget, self).__init__(attrs)
+
+ def render(self, name, value, attrs=None):
+ from django.conf import settings
+ output = []
+ if value:
+ output.append('Currently: <a target="_blank" href="%s%s">%s</a> <br>Change: ' % (settings.MEDIA_URL, value, value))
+ output.append(super(AdminFileWidget, self).render(name, value, attrs))
+ return u''.join(output)
+
class ForeignKeyRawIdWidget(forms.TextInput):
"""
A Widget for displaying ForeignKeys in the "raw_id" interface rather than
2  django/http/__init__.py
View
@@ -48,6 +48,8 @@ def get_host(self):
"Returns the HTTP host using the environment or request headers."
# We try three options, in order of decreasing preference.
host = self.META.get('HTTP_X_FORWARDED_HOST', '')
+ if host:
+ return host
if 'HTTP_HOST' in self.META:
host = self.META['HTTP_HOST']
else:
Please sign in to comment.
Something went wrong with that request. Please try again.