Easy configuration of Django's static files

Source: Internet
Author: User
Tags file url

http://blog.csdn.net/hireboy/article/details/8806098

After Django 1.3, static files are handled statically, such as the CSS JS images of the website, and so on, and the previously so-called media files are changed to deal with the outside of the website itself. For media and static use, please go to the previous "understanding of Django Media and Static Concepts ".

If you simply use static, you can configure it one step less than media, and you'll compare it below. When it comes to static, the difference between the Django development environment and the deployment environment is that the debug variables in the Django Config file settings.py are the main difference, debug=true, in the development environment (debug mode), debug= False instead of being used in a formal deployment environment. There are a lot of differences between the two, such as caching, how the error message is handled, and how to handle the static file today. Static files in the development environment are handled by the Django-brought Web server (which is more). If debug is set to False, then Django's own Web server naturally does not handle static files, and static files are handed to Nginx,apache to handle it (which is more efficient).
Specifically, static in the development environment how to configure it.
This time the main Django 1.4 version to do the configuration, this should be stated beforehand, because after 1.4, Django project structure has undergone great changes, if the following configuration practices put on Django 1.3 certainly does not adapt, it is worth noting oh. The STAITC configuration can only be done in settings.py.
Debug=true This setting environment is in the development environment.
Then build a variable site_root yourself, the variable is not necessary, just for the sake of reuse, the name can be arbitrary. Import OS Site_root=os.path.join (Os.path.abspath (Os.path.dirname (__file__)), '. ') variable value the root of the project, which is where the settings.py is stored.
Then assign a value to the static file variable and tell Django where the static file is Static_root = Os.path.join (site_root, ' static ')
Speaking of which, I would like to talk about the project file structure, which is easy to chiseler, such as the project structure is as follows: MyProject----myproject--------__init__.py--------settings.py-------- urls.py--------wsgi.py----Blog--------__init__.py--------models.py--------views.py--------test.py----Static--- -----CSS------------style.css--------js------------jquery.js--------Images------------me.jpg----Media-------- Upload
Pay attention to the structure of the project and you will find that Site_root is the path to MyProject----MyProject.
Static_root is the path to myproject----media.
OK, then configure said Static_url = '/static/' to the static file URL a suffix, used in the templates.
The final key part is staticfiles_dirs the following configuration Staticfiles_dirs = (("CSS", Os.path.join (static_root, ' CSS ')), ("JS", Os.path.join (ST Atic_root, ' JS '), ("Images", Os.path.join (Static_root, ' images ')), briefly say, the STATIC folder in the project, there is CSS JS images three folders (see project structure), he Our paths are: Os.path.join (static_root, ' CSS '), Os.path.join (Static_root, ' JS '), Os.path.join (Static_root, ' images '); We gave them three aliases css,js,images (you can give, but for easy to remember, we have the original name to specify the alias)
Here STAITC is configured to complete, do not need to urls.py in the configuration related things, Django will automatically find the path to resolve static files, but also in the configuration of static than media a step less.

Configured, the static files can be used normally in templates. How to use it? Take a templates to explain it.

<! DOCTYPE html>
<meta charset=utf-8>
&LT;TITLE&GT;STAITC Example </title>
<link rel= "stylesheet" type= "Text/css" href= "{{static_url}}css/style.css" media= "screen"/>
<script type= "Text/javascript" src= "{{static_url}}js/jquery.js" ></script>
<body>

</body>

Note {{Static_url}} is the settings.py variable in the config file Static_url, the parsing is:/ static/.
and { Static_url}}css/style.css finally resolves what we want:/static/css/style.css,django will find the appropriate file based on the configuration file.


come to the end, talk about the formal deployment environment how to set it:
debug=false
first set Debug to False , tells the Django environment to be a formal non-debug mode, and Django will no longer handle static pages by itself, and finally the static files are handed to nginx Apache for processing.

said Nginx How to configure, to correctly parse the static file, in a server to add a location static file processing

location  /static/{
        root  /home/www-data/twogoo/myproject/;

   }

There's an easy place to make mistakes. , is the path, some students often specify such a path:/home/www-data/twogoo/myproject/staitc/, so nginx should not find static files, because Nginx will run to/home/www-data/twogoo/ myproject/staitc/staitc/Path, note this, static file processing you will be much smoother.

Easy configuration of Django's static files

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.