Using CSS in a Django template, JavaScript

Source: Internet
Author: User
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 '

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.