Opened 4 months ago

Closed 4 months ago

Last modified 4 months ago

#32991 closed Bug (invalid)

Change in behavior of FileField.url between versions 2.2.16 -> 3.2.5; returned 'relative' value contains leading slash

Reported by: Elchin Mammadov Owned by: nobody
Component: contrib.staticfiles Version: 3.2
Severity: Normal Keywords: FileField.url
Cc: Florian Apolloner Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


Going through an upgrade of Django from version 2.2.16 to 3.2.5, believe I have found a regression in FileField.url

I have searched all the release notes between these two versions for any mentions of "url" but didn't find changes being mentioned to this behavior. Some of the recent release notes for Django 2.2 (2.2.21 and 2.2.23) mention changes to FileField, maybe this is when the regression was introduced.

Details of the issue:
Using the same model FileField definition and settings in both versions.

In 2.2.16 FileField.url returns a relative URL without a leading slash.
In 3.2.5 the same call returns a relative URL with a leading slash.

How to reproduce:

Using the following Model definition:

from django.contrib.gis.db import models

class TestModel(models.Model):
    file_field = models.FileField(

Using shell:
In 2.2.16

>>> test_model = models.TestModel.objects.create()
>>> f = open('')
>>> from django.core.files.base import File
>>>'new_name', File(f))
>>> test_model.file_field.url
>>> import django
>>> django.__version__

In 3.2.5:

>>> from vault import models
>>> test_model = models.TestModel.objects.create()
>>> f = open('')
>>> from django.core.files.base import File
>>>'new_name', File(f))
>>> test_model.file_field.url
>>> import django
>>> django.__version__

The problem with the leading slash is that it is now treated as an absolute URL.

Change History (6)

comment:1 Changed 4 months ago by Mariusz Felisiak

Component: File uploads/storagecontrib.staticfiles
Resolution: invalid
Status: newclosed

This is an intended change made in c574bec0929cd2527268c96a492d25223a9fd576 (see #25598, and comment).

comment:2 Changed 4 months ago by Mariusz Felisiak

Cc: Florian Apolloner added

comment:3 Changed 4 months ago by Florian Apolloner

Interesting, this is honestly a somewhat unexpected result. While the docs do say (

A read-only property to access the file’s relative URL by calling the url() method of the underlying Storage class.

that this seems to be a relative URL, it also says that it calls Storage.url which to the best of my knowledge usually always returned an absolute URL ( -- well technically it returned whatever MEDIA_URL + "/" + path is (more or less):

    def url(self, name):
        if self.base_url is None:
            raise ValueError("This file is not accessible via a URL.")
        url = filepath_to_uri(name)
        if url is not None:
            url = url.lstrip('/')
        return urljoin(self.base_url, url)

so the only way I could imagine this to ever return a relative URL is by settings MEDIA_URL to an empty string. What did you set MEDIA_URL to (and if it is empty, then why)

comment:4 Changed 4 months ago by Elchin Mammadov

MEDIA_URL is indeed an empty string in our settings, which is also the default value.

comment:5 Changed 4 months ago by Florian Apolloner

It is true that it is the default value but that was never a useful value to begin with if you deployed your site. Assume the following (with the default MEDIA_ROOT): you are on page and request the URL of my_image.jpg which would yield photos/123/my_image.jpg. Putting that into an <img> tag would result in a final URL of Do the same on instead and you'd get So how did you ever serve your images with this setup?

comment:6 Changed 4 months ago by Elchin Mammadov

I see your point about the default value of MEDIA_URL not being useful when using .url for serving static content from the site. In this use case MEDIA_URL must be configured. This may be the most common use case.

The other use case to consider is when uploading files for consumption by the web application. After the files have been uploaded, the application has a need to find a relative path to the uploaded file via a model. And Django FAQ documentation actually describes how to achieve this

All that will be stored in your database is a path to the file (relative to MEDIA_ROOT). You’ll most likely want to use the convenience url attribute provided by Django

What is returned by .url attribute is also a relative path to the file, which the application can then access via the FileField on the model. If there is no static content being served, then MEDIA_URL will not be set (most likely). This is the use case and the setup used in our application. We are not using Django templates or serving any static content. In our application there are only a few places where .url attribute is being used, so it is a simple change for us. There are other attributes provided by FileField that can be used in lieu of .url.

Last edited 4 months ago by Elchin Mammadov (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top