[Django] #29570: Document that MEDIA_URL inside STATIC_URL leads to unexpected behaviour in the development server

10 views
Skip to first unread message

Django

unread,
Jul 17, 2018, 4:48:52 AM7/17/18
to django-...@googlegroups.com
#29570: Document that MEDIA_URL inside STATIC_URL leads to unexpected behaviour in
the development server
-----------------------------------------------+------------------------
Reporter: Alejandro Dubrovsky | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 2.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-----------------------------------------------+------------------------
Setting a MEDIA_URL that is within STATIC_URL, eg

'''
STATIC_URL = '/static/'
MEDIA_URL = os.path.join(STATIC_URL, 'upped') + '/'
'''
leads to the development server refusing to serve media files when
staticfiles is installed even if a URL route is specified since the
staticfiles.handlers.StaticFilesHandler takes over.

While there is a check in staticfiles warning that MEDIA_URL should not be
equals to STATIC_URL, it does not warn about the cases where STATIC_URL is
a prefix of MEDIA_URL. This also does not seem to be documented anywhere
that I've seen.

The suggestion to allow this seems to have been closed wontfix in
https://code.djangoproject.com/ticket/15199 so I won't attempt a patch.
Adding a check for this case does seem easy though, and I've attached a
patch that adds a warning to this ticket. The documentation could also
mention that adding an explicit url pattern for static and running
```python manage.py runserver --nostatic``` can be used as a workaround.

--
Ticket URL: <https://code.djangoproject.com/ticket/29570>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 17, 2018, 4:49:12 AM7/17/18
to django-...@googlegroups.com
#29570: Document that MEDIA_URL inside STATIC_URL leads to unexpected behaviour in
the development server
-------------------------------------+-------------------------------------

Reporter: Alejandro Dubrovsky | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 2.0
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Alejandro Dubrovsky):

* Attachment "staticwarning.patch" added.

Django

unread,
Jul 18, 2018, 3:58:10 AM7/18/18
to django-...@googlegroups.com
#29570: Add check that MEDIA_URL is not inside STATIC_URL.
--------------------------------------+------------------------------------

Reporter: Alejandro Dubrovsky | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Core (System checks) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Carlton Gibson):

* component: Documentation => Core (System checks)
* version: 2.0 => master
* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

> The documentation could also mention that adding an explicit url
pattern for static and running python manage.py runserver --nostatic can
be used as a workaround.

The conclusion from #15199 was that having MEDIA_URL inside STATIC_URL
(MEDIA_ROOT inside STATIC_ROOT?) is a pattern that we want to avoid, so I
don't think documenting a workaround is consistent with that.

Rather, strengthening the system check seems reasonable. (The patch checks
media is not inside static. Is it worth doing the opposite too?)

The MEDIA_ROOT docs already say:

> MEDIA_ROOT and STATIC_ROOT must have different values. ...

With the extra check this ''may'' be enough (I've always read it that way)
but if an alternative phrasing that says "not inside each other too" that
is both short and precise can be found then maybe we can update that as
well.

--
Ticket URL: <https://code.djangoproject.com/ticket/29570#comment:1>

Django

unread,
Jul 18, 2018, 4:22:29 AM7/18/18
to django-...@googlegroups.com
#29570: Add check that MEDIA_URL is not inside STATIC_URL.
--------------------------------------+------------------------------------
Reporter: Alejandro Dubrovsky | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Core (System checks) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Carlton Gibson):

* needs_tests: 0 => 1


Comment:

Alejandro, could you create a Pull Request on GitHub with your patch,
adding at least a test case, so we can review? Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/29570#comment:2>

Django

unread,
Jul 18, 2018, 12:09:12 PM7/18/18
to django-...@googlegroups.com
#29570: Add check that MEDIA_URL is not inside STATIC_URL.
--------------------------------------+------------------------------------
Reporter: Alejandro Dubrovsky | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Core (System checks) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Alejandro Dubrovsky):

Done: https://github.com/django/django/pull/10196

As mentioned in the pull request, the check only runs when staticfiles
takes over (ie in DEBUG mode when staticfiles is in the INSTALLED_APPS),
since that is when the problem occurs and that's what the warning warns
about.

--
Ticket URL: <https://code.djangoproject.com/ticket/29570#comment:3>

Django

unread,
Aug 8, 2018, 10:10:30 AM8/8/18
to django-...@googlegroups.com
#29570: Add check that MEDIA_URL is not inside STATIC_URL.
--------------------------------------+------------------------------------
Reporter: Alejandro Dubrovsky | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Core (System checks) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Herbert Fortes):

* cc: Herbert Fortes (added)


Comment:

Hi,

I ran the tests (./runtests.py) without a problem.

I have one minor suggestion. Instead of `\` use `()` to break a line:
{{{#!python
if (settings.MEDIA_URL and settings.STATIC_URL and
settings.MEDIA_URL.startswith(settings.STATIC_URL)):
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29570#comment:4>

Django

unread,
Aug 12, 2018, 4:57:27 AM8/12/18
to django-...@googlegroups.com
#29570: Add check that MEDIA_URL is not inside STATIC_URL.
--------------------------------------+------------------------------------
Reporter: Alejandro Dubrovsky | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Core (System checks) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Alejandro Dubrovsky):

Modified to use parenthesis instead of a continuation marker to break up
the line as per Herbert's suggestion.

--
Ticket URL: <https://code.djangoproject.com/ticket/29570#comment:5>

Django

unread,
Aug 23, 2018, 10:11:02 AM8/23/18
to django-...@googlegroups.com
#29570: Add check that MEDIA_URL is not inside STATIC_URL.
--------------------------------------+------------------------------------
Reporter: Alejandro Dubrovsky | Owner: nobody
Type: Cleanup/optimization | Status: closed

Component: Core (System checks) | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"108c04f572ccca74258c4d2024aef61270a5b083" 108c04f]:
{{{
#!CommitTicketReference repository=""
revision="108c04f572ccca74258c4d2024aef61270a5b083"
Fixed #29570 -- Added check that MEDIA_URL isn't in STATIC_URL.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29570#comment:6>

Reply all
Reply to author
Forward
0 new messages