Serving static files in Django
From https://docs.djangoproject.com/en/dev/howto/static-files/: For a production site you probably want to configure the server to serve static files directly, without going through Django. For a test site, you can use the following approach
- In settings.py, url for requesting any static files is defined as follows:
STATIC_URL = ‘/static/’- This is the default, so you do not need to add it in, but you may want to change it
- To specify the location of the static files, add the following code to settings.py:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, “static”),
)- Django will now look for static files in (project root)/static
- Create the static file, in my case in <dev root>/Projects/DjangoText/static/testimage.jpg
- In your html template file or generated html, before including a static file, add the following:
{% load staticfiles%} - Include the static file as follows:
<img src=”{% static “testimage.jpg” %}” “/>- Note: When Django sees the “static” marker, it knows to extend the path to include the location of the static folder
- Note the double double quotes
- Refresh the screen to test