#413 closed defect (fixed)
[patch] FileField get_<fieldname>_url returns URL with backslashes on windows
| Reported by: | 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 | 
Description
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/__init__.py
===================================================================
--- django/core/meta/__init__.py	(revision 547)
+++ django/core/meta/__init__.py	(working copy)
@@ -951,7 +951,7 @@
 def method_get_file_url(field, self):
     if getattr(self, field.name): # value is not blank
         import urlparse
-        return urlparse.urljoin(settings.MEDIA_URL, getattr(self, field.name))
+        return urlparse.urljoin(settings.MEDIA_URL, getattr(self, field.name)).replace('\\', '/')
     return ''
 
 def method_get_file_size(field, self):
      
Fixed in [579].