Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#413 closed defect (fixed)

[patch] FileField get_<fieldname>_url returns URL with backslashes on windows

Reported by: Adam Endicott <leftwing17@…> Owned by: Adrian Holovaty
Component: Core (Other) Version:
Severity: normal Keywords: FileField url windows backslash
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I have a model with a FileField: meta.FileField('file', upload_to='uploadDir'), . On windows, when uploading the file, its path is saved in the database as 'uploadDir\filename' (with the backslash). This becomes a problem when using get_file_url() because the url then contains a backslash (in the form of: MEDIA_URL/uploadDir\filename)

The patch changes get_<fieldname>_url() behavior to replace all backslashes with forward slashes before returning (specifically, using .replace('\\', '/') ).

There was some discussion on #django about where to do the replace, here, or where the path is stored in the database, and we decided to do it just when retrieving a URL since the backslash is valid in a path until we start calling it a URL.

Index: django/core/meta/
--- django/core/meta/	(revision 547)
+++ django/core/meta/	(working copy)
@@ -951,7 +951,7 @@
 def method_get_file_url(field, self):
     if getattr(self, # value is not blank
         import urlparse
-        return urlparse.urljoin(settings.MEDIA_URL, getattr(self,
+        return urlparse.urljoin(settings.MEDIA_URL, getattr(self,'\\', '/')
     return ''
 def method_get_file_size(field, self):

Change History (1)

comment:1 Changed 13 years ago by Adrian Holovaty

Resolution: fixed
Status: newclosed

Fixed in [579].

Note: See TracTickets for help on using tickets.
Back to Top