#32991: Change in behavior of FileField.url between versions 2.2.16 -> 3.2.5;
returned 'relative' value contains leading slash
-------------------------------------+-------------------------------------
Reporter: Elchin Mammadov | Owner: nobody
Type: Bug | Status: closed
Component: contrib.staticfiles | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: FileField.url | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Florian Apolloner):
Interesting, this is honestly a somewhat unexpected result. While the docs
do say
(https://docs.djangoproject.com/en/3.2/ref/models/fields/#django.db.models.fields.files.FieldFile.url)
> 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
(https://docs.djangoproject.com/en/3.2/ref/files/storage/#django.core.files.storage.Storage.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)
--
Ticket URL: <https://code.djangoproject.com/ticket/32991#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.e18e053828d80738fe22df5d046849f3%40djangoproject.com.