Using Django to handle requests for static files

Source: Internet
Author: User

Introduction

This method applies to linux+python2.7+django1.2, using Django's own Web service.

Also applies to Sina app engine.

1. Preparatory work

Prepare the CSS file, prepare the JS file, and prepare the image file.

Create a new directory "medias" at the site root and create a new directory "Styles" in the "medias" directory to store the CSS files.

2. Configure the path for static file access 2.1. Add a directory of static files in the settings.py file

Assuming MySite is the root of the site, medias is the new directory we just created

?
1234567 STATICFILES_DIRS = (    # Put strings here, like "/home/html/static" or "C:/www/django/static".    # Always use forward slashes, even on Windows.    # Don‘t forget to use absolute paths, not relative paths.    ‘mysite/medias‘   )
2.2. Add the following routing rule in the urls.py file?
1234567 urlpatterns=patterns ( Code class= "CSharp string" > " ,        (R ' ^site_medias/(? p<path>.*) $ ' Django.views.static.serve '           { Document_root ' :mysite.settings.staticfiles_dirs, ' show_indexes ' &NBSP;&NBSP;&NBSP;&NBSP;  < Code class= "CSharp spaces" >&NBSP;&NBSP;&NBSP;&NBSP;  )

Maps a request to a file for the Site_medias directory and its subdirectories to the path specified in the staticfiles_dirs of the settings.py file settings. and using the Show_indexes parameter, after the parameter is set to True, indicates that the resources of the Medias directory can be browsed through the browser, including browsing the folder and its files. The browse address is http://localhost:8080/site_medias/, assuming that the service uses a port of 8080, after setting the above option, you should be able to browse through the folder and file through that address.

3. Using static files 3.1. Referencing CSS files in the template file

Add the following CSS reference in the head tag of the base.html file.

?
1 <link href="/site_medias/styles/site.css"rel="stylesheet"type="text/css">
3.2. Referencing a CSS file in a normal HTML file

It also adds the following content directly to the head tag of the HTML file.

?
1 <link href="/site_medias/styles/site.css"rel="stylesheet"type="text/css">

Summary

After you finish the work above, the CSS should start to work. The reference to the picture and JS is a similar practice.

On the Django website there are two links to the relevant content, the details of which you can click to view.

https://docs.djangoproject.com/en/1.2/howto/static-files/

The topic of the above link is: How to serve static files, that is, how to use the static file (including Css,js scripts, pictures, etc.). In this, you can use the above method of static files, but Django is mainly used to deal with dynamic files, the above method is not efficient, and unsafe, so it is not recommended. It can be used in the development environment, but the production environment, it is recommended to use professional Web services to provide static file processing. Professional Web services include: Appache, LIGHTTPD, Cherokee, and more. Users who combine these professional web services also have links in the article.

https://docs.djangoproject.com/en/dev/howto/static-files/

The link above is how to use Django to handle static files, some processing details, including configuration Ah, reference ah what.

Using Django to handle requests for 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.