Code

Opened 9 years ago

Closed 9 years ago

Last modified 8 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
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: UI/UX:

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

Attachments (0)

Change History (1)

comment:1 Changed 9 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

Fixed in [579].

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.