[Django]-Django app doesn't recognize static files

2đź‘Ť

Replace

urlpatterns += staticfiles_urlpatterns()

by

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

staticfiles_urlpatterns() is used only for debug to have a display view for your static file. As mentioned on https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/ it should never be used in production.

Note: STATIC_ROOT & MEDIA_ROOT are used only when you’re collecting the static (collectstatic with manage.py). It specifies the absolute path where to store the static and media files (usually /var/www/myapp/static and /var/www/myapp/media)

👤openHBP

1đź‘Ť

In your urls.py you forgot to say how to “compute” the static route files.

Instead of this:

urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Try this:

from django.views import static

urlpatterns = [
    # [...]
    url(r'^public/(?P<path>.*)$', static.serve, {
        'document_root': settings.MEDIA_ROOT
    }, name='url_public'),
]

and in your template files (you didn’t give a sample), try:

{% load static %}
<h1>{% static 'calendar.js' %}</h1>

Dont forget to use PyCharm because it helps so much for such things (it recognizes the path and you’ll get the autocompletion in the template files (Ctrl+Space)).

👤Olivier Pons

Leave a comment