#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 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
https://docs.djangoproject.com/en/3.2/faq/usage/#how-do-i-use-image-and-
file-fields:
> 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.
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`.
--
Ticket URL: <https://code.djangoproject.com/ticket/32991#comment:6>
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.2186172b4459eaa5f787b428192bf312%40djangoproject.com.