Tuesday, October 29, 2019

Serving non-referenced static files

Hello again, 

I could use some help on loading static files in development mode. Specifically, I'd like to tell Django to serve additional files that are not explicitly loaded in the html with {% load static %}. My issue is actually identical to this post, where I could use some extra explanation on their suggested fix. To restate the issue, I load a static javascript file within my myapp/index.html:

{% load static %}
<script src="{% static 'myapp/myscript_1.js' %}"></script>

The javascript file (myscript_1.js) references another javascript file (myscript_2.js) as well as some other media (e.g. a number of *.png, *.css, *.json files). However, Django doesn't serve these files because they are not directly loaded in the html, and a 404 error is thrown when trying to find myscript_2.js - along with the other files. How do I get Django to serve these files as well? 

It looks like Django is searching for the files at http://127.0.0.1:8000/myapp/myscript_2.js. Could I forward this path to http://127.0.0.1:8000/static/myapp/myscript_2.js? I'm trying to do this with the following in my urls.py (in projects urls.py - not the myapp urls.py):

from django.conf import settings
from django.conf.urls.static import static
urlpatterns
+= static(path=settings.STATIC_URL, document_root=settings.STATIC_ROOT)

and in my settings.py:

STATIC_ROOT =  os.path.join(BASE_DIR, 'static')
STATIC_URL
= /static/

This isn't working for me though, and I think I'm using the static() function incorrectly. Could someone offer some guidance on how to set this up for development testing?

Thanks in advance,
Jack


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9de51c2c-4dca-454a-8057-db9c0e8c8b37%40googlegroups.com.

No comments:

Post a Comment