Static file Management in Django

Source: Internet
Author: User

A site usually needs to save extra files, compared to slice CSS style files js script files, in Django, tend to refer to these files as static files. Django provides a django.contrib.staticfiles module to help us manage static files conveniently.

There are two ways to configure static files:

1 Configure a static file under a separate app, such as a separate picture under an app.

2 Configure static files under entire project for files that are not associated with individual apps, such as jquery bootstrap, etc.

Configuration steps:

1 First, we need to confirm the existence of the Installed_apps variable in the settings.py file Django.contrib.staticfiles

1Installed_apps = (2     'Django.contrib.admin',3     'Django.contrib.auth',4     'Django.contrib.contenttypes',5     'django.contrib.sessions',6     'django.contrib.messages',7     ' Django.contrib.staticfiles ',8)

2.1 Perform this step if we want to configure a static file under a separate app.

Define the Static_url variable in the settings.py file.

1 ' /static/ '

2.2 Perform this step if we want to configure static files under the entire project.

Defining Staticfiles_dirs variables in the settings.py file

1 staticfiles_dirs = (2     "static"), # Preferred project static file search path 3     '/var/www/static/',   # The second selection of the project static file search path, you can also have a third choice, the fourth choice ... 4 )

3.1 Perform this step if we want to configure a static file under a separate app.

At this point, we need to create a folder named Static in the app.

3.2 Perform this step if we want to configure static files under the entire project.

We need to create a folder named static throughout project

4 When we are in the template either using the hard link /static/myexample.jpg or using the static label will first access the Myexample.jpg file under the static/folder under the app, and if the file does not appear in the static folder under the app, access the static/folder in project. Check if there is a myexample.jpg file, if any, then return, if not then go to/var/www/static/to find.

  

Static file Management in Django

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.