I am having difficulty loading static files in django2.0 (coming from django1.4). Here is what I have so far:
# urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
# settings.py
TEMPLATES = [
...
'builtins': [
'django.contrib.staticfiles.templatetags.staticfiles',
],
]
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = ''
STATICFILES_DIRS = [
os.path.join(SITE_ROOT, "static"),
]
And I have one image located at:
[my_project_root]/static/img/image.png
Now if I go directly to the url, I get a 404:
http://localhost:8000/static/img/image.png
Additionally, if I do it "through the template", I also get a 404:
{% load static %}
<img src="{% static 'img/image.png' %}" alt="My image">
What do I need to add here to serve the static files?
This was a tricky setting. I needed to change the STATIC_ROOT
to:
STATIC_ROOT = 'static/'
Otherwise, with STATIC_ROOT
= '', it would look for the img file in [project_root]/img/image.png
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.