see: The staticfiles app | Django
- open settings.py and set
STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, "static")
- add your app in
INSTALL_APP
and store your static files in the application subdirectory namedstatic
project/app/static/example.jpg
- add your app in
- collect all static files
django-admin collectstatic
- config web server to server static files
- default web server of Django (invalid whe
DEBUG = False
) - add the url pattern of static files:
from django.conf import settings from django.conf.urls.static import static urlpatterns = [ # other URL conf ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
- default web server of Django (invalid whe
- IIS + wfastcgi
- make a virtual directory
- delete the mapping to static dir
- use the
static
tag in your templates {% load static %} <img src="{% static "my_app/example.jpg" %}" alt="My image"/>
- use the
Last edit: 2018-05-21 19:37:04
see: Customizing error views | Django
- open
settings.py
and set: TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { # other options }, } ]
note
APP_DIRS: True
would enable the engine to look for templates inside installed applications in order. it is conventional that templates stored in application subdirectory namedtemplates
- open
- add your app in
INSTALL_APP
and store your template files in the application subdirectory namedtemplates
project/app/templates/example.html
- add your app in
- use template in views
from django.shortcuts import render def my_view(request): # other code return render(request, 'example.html', {'foo': 'bar', }, content_type='application/xhtml+xml')
the example equal to
from django.http import HttpResponse from django.template import loader def my_view(request): # other code t = loader.get_template('app/example.html') c = {'foo': 'bar'} return HttpResponse(t.render(c, request), content_type='application/xhtml+xml')
note that specify
app/example.html
when multiple files are available.
- django automatically load
404.html
as the 404 error page whenDEBUG = False
so that you can move your custom 404 page toapp/templates/404.html
. alternative solution is custom error views. i. specify the handler as seen below in your URL conf (setting have no effect anywhere else)
handler404 = 'app.views.custom_page_note_found_view'
Source code for django.views.defaults.page_not_found | Django
- django automatically load
Last edit: 2018-05-21 22:49:23
Can't serving static files?
- can't load CSS, JS, img, cause Django can't find your static files. You can execute the following command to display the search path when Django load static files.
python manage.py findstatic --verbosity 2 css/styles.css
- once static files outside the search locations, you can increase the searching locations, and also move file to exists locations. there are two method to increase locations:
- add app name to the setting list named
INSTALLED_APP
insettings.py
. and the new location isproject/app_name/static/
INSTALLED_APP = [ # others app, 'app_name', ]
- add app name to the setting list named
- add new location to
STATICFILES_DIRS
insettings.py
. this setting in invalid whenDEBUG = False
, and conflict to another setting namedSTATIC_ROOT
when the same location existed. STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'staticfiles'), )
- add new location to
last edit: 2018-05-21 19:37:10