Code

Ticket #7415: 7415-filefield-url-admin-windows.diff

File 7415-filefield-url-admin-windows.diff, 1.9 KB (added by julien, 6 years ago)

Patch and tests

Line 
1Index: django/django/contrib/admin/widgets.py
2===================================================================
3--- django/django/contrib/admin/widgets.py      (revision 7968)
4+++ django/django/contrib/admin/widgets.py      (working copy)
5@@ -3,6 +3,7 @@
6 """
7 
8 import copy
9+import urllib
10 
11 from django import newforms as forms
12 from django.newforms.widgets import RadioFieldRenderer
13@@ -88,8 +89,8 @@
14         from django.conf import settings
15         output = []
16         if value:
17-            output.append('%s <a target="_blank" href="%s%s">%s</a> <br />%s ' % \
18-                (_('Currently:'), settings.MEDIA_URL, value, value, _('Change:')))
19+            output.append('%s <a target="_blank" href="%s">%s</a> <br />%s ' % \
20+                (_('Currently:'), urllib.pathname2url('%s%s' % (settings.MEDIA_URL, value)), value, _('Change:')))
21         output.append(super(AdminFileWidget, self).render(name, value, attrs))
22         return mark_safe(u''.join(output))
23 
24Index: django/tests/regressiontests/admin_widgets/models.py
25===================================================================
26--- django/tests/regressiontests/admin_widgets/models.py        (revision 7968)
27+++ django/tests/regressiontests/admin_widgets/models.py        (working copy)
28@@ -50,6 +50,11 @@
29 >>> print conditional_escape(w.render('test', 'test'))
30 Currently: <a target="_blank" href="%(MEDIA_URL)stest">test</a> <br />Change: <input type="file" name="test" />
31 
32+In Windows, make sure that backward slashes are turned into forward slashes in the url.
33+>>> w = AdminFileWidget()
34+>>> print conditional_escape(w.render('testname', 'path\\\\file'))
35+Currently: <a target="_blank" href="%(MEDIA_URL)spath/file">path\\file</a> <br />Change: <input type="file" name="testname" />
36+
37 >>> band = Band.objects.create(pk=1, name='Linkin Park')
38 >>> album = band.album_set.create(name='Hybrid Theory')
39