Django: static image not loading
I have been following the official documentation for the letter trying to find some advice on related issues here and just browsing the web in general and I still can't get one small image to download.
I have an image called "logo.png" and my project hierarchy looks like this:
project/
mysite/
app/
__init__.py
admin.py
models.py
urls.py
view.py
mysite/
__init__.py
settings.py
urls.py
views.py
wsgi.py
static/
logo.png
templates/
index.html
calling_image.html
Inside settings.py
I haveSTATIC_URL = '/static/'
Inside calling_image.html
I have<img src="/static/logo.png">
My template calling_image.html
is called project/mysite/app/views.py
, which of course is then called project/mysite/app/urls.py
, and I even tried to include the following two lines (as I said several times) at the end of mine urls.py
:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
Nothing works, so what did I do wrong?
Edit . Sorry I did the type I have STATIC_URL = '/static/'
with a closing forward slash in mine settings.py
and for clarification, I run this on my dev build withpython manage.py runserver
Resolved . So I decided it myself. I created a directory resources
inside project/mysite/
and placed it there logo.png
. Then I installed STATICFILES_DIRS = (os.path.join(BASE_DIR, "resources"),)
and ran collecstatic
and it worked! There was no need to use urlpatterns += staticfiles_urlpatterns()
anything else.
source to share
It sounds like you probably want a variable STATICFILES_DIRS
defined in your settings.py and that should include the location of that directory static
containing logo.png
.
Django by default only looks for static files inside a directory static
inside each application. If you have a static file outside of any application, it won't be automatically loaded with collectstatic
.
See https://docs.djangoproject.com/en/1.8/ref/settings/#std:setting-STATICFILES_DIRS
Combining Confusion Static Files
There is always a lot of confusion about Django and Static. I believe this confusion comes from the fact that Django doesn't make sense to handle static files in production , but naturally use it when learning how to use Django and thus in initial development.
The reason Django doesn't make sense to serve static files is because Django is the framework for rendering dynamic responses. "Static" files are not dynamic: they do not change. So it is a huge waste of resources to deploy a Python application that calls all of Django's mechanisms to find and return a file that has no dynamic content.
Static Files: Web Server Operation
There is something that serves static files really well: the web servers themselves (such as Apache, nginx, etc.). This is what they were meant to do. The web server can run a Python / Django app, or it can just find the file and send it back, so you usually set up the web server by telling it something like the following (in a pseudo server):
When someone refers to the way
/static/
they can access files in the following directory:/var/www/static/
.When someone refers to a path
/
, expand this Python application that lives here:/var/www/django-app
.
Django static file tools
As a result, Django comes with some helper tools that manipulate static files so that your actual server can serve them.
These tools are as follows (defined in settings.py
):
-
STATIC_URL
: URL path where your server will serve your static files. It's just that when you usestatic
templatetag this Django knows howurlreverse
it is. In other words, it's just a convenient way to turn{% static "..." %}
into/static/...
. -
STATIC_ROOT
: a place on your server (or in the cloud somewhere) that Django will copy your static files to, so that your server can serve them. This copying happens when you startcollectstatic
. -
STATICFILES_DIRS
: any additional directories Django should look for static files whenever you startcollectstatic
. From default, Django only scans in each app directory forstatic
(as withtemplates
).
Static files in development
Ok, but it's not that useful in development, where you are probably using the Django command runserver
. You do not have a server that will host the server's static files.
Thus, you can ask Django to please also the server static files for you in only one case, because you are developing your application and don't want to run a separate server.
There is a view that should automatically collect and serve static files when DEBUG=True
. Alternatively, this also explains why someone might use the following:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
source to share
If I remember, you need to point STATIC_ROOT = 'static'
to settings.py
, and you need to provide a url to point /static
to your static files. So.
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT, 'show_indexes': True
}),
)
This may be a little dated, but still works for me.
Also are you trying to get this to work on your dev site from python manage.py runserver
or on a production site?
Update
Here is an example of the main urls.py file.
from django.conf.urls import patterns, include, url
urlpatterns = (
url(r'^admin/', include(admin.site.urls)),
# more urls...
)
#The following enable structural 'static' files while in development mode.
if settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT, 'show_indexes': True
}),
)
patterns
imported at the top of your urls.py file.
Here is STATIC_ROOT
.
You may also need to run python manage.py collectstatic
so that your static files are compiled from your various applications and copied to a folder STATIC_ROOT
.
See this answer for a more detailed explanation :)
source to share
- Make sure to
django.contrib.staticfiles
turn onINSTALLED_APPS
. - In your settings file define STATIC_URL, for example:
STATIC_URL = '/static/'
- Hope you saved the images in the folder
/static/projectname/image.png
- Finally, in your html just add the following
<img src="/static/projectname/image.png" alt="My image"/>
source to share