Opened 19 years ago

Closed 19 years ago

Last modified 18 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

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):

Change History (1)

comment:1 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: newclosed

Fixed in [579].

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