have been optimizing the code of their own blog, yesterday, the CSS style sheet was separated, as a static
File, but didn't learn how Django uses static files, after some google
Finally solved.
There are two ways Django uses static files, one that is deployed on Django, and the other
is deployed on Apache, the previous method has a certain flaw, inefficient, and will create
To a certain security threat, I do not know, you can read the official documents, but still
Let me introduce you to you.
- You can build a static directory under your project and add it in settings.py:
#填写你静态文件的绝对路径
Static_path = '/your Project path/static/'
- Open Url.py:import Settings
Then add in the Urlpatterns:
(r‘^static/(?P<path>.*)$‘,‘django.views.static.serve‘, {‘document_root‘:settings.STATIC_PATH})
- Finally, modify the template, where the static files are needed, modify the code to:
But I think the second method is more simple, a lot of information on the Internet is not very clear, I figured out
Here:
- Configure the files in the Mod_wsgi, which is your previously configured script:
If it's Ubuntu, that should be the http.conf file, and the Fedora Word is the wsgi.conf file
Just press divert, just like a static file that adds a Django admin interface, plus
Alias /static/ /usr/www/your project/static<Directory "/static/"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing</Directory>
The code means that you build a static directory under your project's directory and then put your static files
Put it in there.
Finally, modify the place where you want to load the static files, such as the CCS stylesheet:
<link rel="stylesheet" type="text/css" href="/static/your css file" />
To deploy Django static files on Apache