Using CSS in a Django template, JavaScript
(R ' ^css/(? ppath.*) $ ', ' Django.views.static.serve ', {' document_root ': '/var/www/django-demo/css '}),
(R ' ^js/(? p/pathpath.*) $ ', ' Django.views.static.serve ', {' document_root ': '/var/www/django-demo/js '}),
(R ' ^images/(? p/pathpath.*) $ ', ' Django.views.static.serve ', {' document_root ': '/var/www/django-demo/images '}),
You can use the following method in the template:
Link href= "/css/demo.css" type= "Text/css" rel= "stylesheet"
Note: You can use Os.path.dirname (Globals () ["__file__"]) to get the path of the current file, such as
(R ' ^css/(? ppath.*) $ ', ' Django.views.static.serve ', {' Document_root ': Os.path.dirname (Globals () ["__file__"]) + '/css '}),
You can use the Os.path.abspath () function to return an absolute path to this path.
==============
In the Django Tempalte file to reference the CSS, JS, GIF and other static files, first a setting.py in the debug switch open.
1. Create a directory in the project directory that holds static files, such as: medias
2. Add a line in url.py patterns:
(R ' ^site_media/(? ppath.*) $ ', ' Django.views.static.serve ', {' Document_root ': Settings. Static_path}),
Also from django.conf import setting
3. Add a line to the setting.py:
Static_path= './medias '
Once this is set up, you can reference the static files stored in media in the template file, such as:
IMG src= '/site_media/django.gif '