Django Basics, Day9-static file directory with path setup instructions (eg. images, JavaScript, CSS)

Source: Internet
Author: User

Static file path Setting official description

1. Make sure this django.contrib.staticfiles is included in your Installed_apps.

2. In your settings file, define Static_url, for example:

Static_url = '/static/'

3. Store your static files in a folder called the static in your app. For example My_app/static/my_app/myimage.jpg.

4. In your templates, either hardcode the URL like/static/my_app/myexample.jpg or, preferably, use the static template Ta G to build the URL for the given relative path by using the configured staticfiles_storage STORAGE (This makes it Much easier when you want to switch to a content delivery network (CDN) for serving static files).

{% load staticfiles%}
Working with instances

In the previous example, we used the third method above, in the app directory, created the static directory, which also created the name of the same as the name of the app directory, the only place static resources files, the path such as:

POLLS/STATIC/POLLS/STYLE.CSS, used when introduced in template

{% load static%}<link rel= "stylesheet" type= "Text/css" href= "{% static ' polls/style.css '%}"/>

One, Django project default read static file directory such as/projectname/appname/static/

Second, if we do not want to put static files in the app or even under the project directory, you can set the staticfiles_dirs in settings.py, such as:

Staticfiles_dirs = (    ' d://static/',    # os.path.join (Base_dir, "static"),    # '/var/www/static/',)

Then the previous example, when creating D://STATIC/POLLS/STYLE.CSS

Li a {    color:red;} Body {    background:white url ("images/background.jpg") no-repeat center Top;}

Refresh the page to find that the text color in the link is changed from the original green to red, indicating that the CSS path referenced by the page takes effect

Django Basics, Day9-static file directory with path setup instructions (eg. images, JavaScript, CSS)

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.