There are two files that need to be modified for routing configuration (static file path configuration) in Django:
Generally we will put all static files in a folder, so now we need to create a new static directory in the project directory , and then place all the static files needed for the website: CSS, pictures, JS, etc. in the static directory.
Note: The configuration methods of Django 1.4 and 1.3 are slightly different.
Static file path configuration
1 in Django 1.4, add in urls.py:
url(r'^static/(?P
Django’s own static file server is used here for processing. It is no longer needed after it is deployed to the production environment.
2, settings.py
ROOT_PATH = os.path.normpath(os.path.dirname(__file__)).replace('\','/')
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os .path.join(ROOT_PATH,'../templates')
)
Let’s talk about the static file path configuration of Django1.3:
1. Set the static file path
We need to set it in the settings.py file Set a static file path STATIC_PATH, which is the directory where we just stored the static files. In order to avoid hard-coding the path, we can use some methods in the OS module to convert the absolute path. Add the following code to the settings.py file:
import os
your_path=lambda *x: os.path.join( os.path.abspath(os.path.dirname(__file__)),*x)
STATIC_PATH=your_path('static')
We need to use this STATIC_PATH when configuring urls.
2. Configure static file urls
In the urls.py file we add the following code:
from django.conf import settings
url(r'^static/(?P
Of course, we can use other names for the "static" in the urlconf. By convention, we generally use "static"
One last thing to note: everything is configured, now if we want to use static files in template files, we can call them normally. Please note that "/" must be added at the beginning of the path