Django Official tutorial (eight) "Create your first Django project, Part VI"

Source: Internet
Author: User
Create your first Django project, part sixth

This article continues from the end of the fifth part (EN). We've written a test for the voting program, and now we're going to add styles and pictures to it.

In addition to the server-generated HTML, network applications typically require some other files-such as pictures, scripts, and stylesheets-to help render the Web page. In Django, we collectively refer to these files as "static files."

This is no big deal for small projects, because you can put these static files anywhere, as long as the service program can find them. However, in large projects-especially large projects made up of several applications-it is a bit cumbersome to work with the static files needed for different applications.

This is the meaning of django.contrib.staticfiles 's existence: it collects the static files of each application (and some of the files in the directory you specify) so that in a production environment these files will be concentrated in a convenient place to distribute. customizing the application's interface and style

First, create a static directory under the polls directory. Django will collect static files from here, just as Django looks for templates in polls/templates .

The Django staticfiles_finders setting is a list that contains a number of searchers who know how to find static files from different locations. One of these is Appdirectoriesfinder, which looks for files from the "static" subdirectory of each application in Installed_apps , such as just created polls/static . The previous Administration page also uses the same directory structure to manage static files.

Create a polls directory under the static directory you just created, and then new file style.cssin it. In other words, the location of your style file is polls/static/polls/style.css. Because of the existence of Appdirectoriesfinder , you can use polls/style.css in Django to refer to this file, just as we refer to the template file.

Static file namespaces

As with templates, although we can put the stylesheet directly under the polls/static directory (instead of creating a polls subdirectory), this is not a good idea. Django will use the first and name items it finds to match the file, and when you have a file with the same name in another application, Django will not be able to differentiate between them. The easiest way to get Django to find the right file is to use namespaces. That is, the static file is placed in a subdirectory with the same name as the application.

Write the following code to the style sheet (polls/static/polls/style.css):

/* polls/static/polls/style.css

/li a {
    color:green
}

Next, add the following at the top of the polls/templates/polls/index.html :

<!--polls/templates/polls/index.html-->

{% load static%}

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

The {% static%} statement converts a reference to a static file to an absolute address.

This is all the work you need to do. Now reload the http://localhost:8000/polls/, and you'll see that the problem link turns green (Django uses the style), which means the stylesheet is working properly. Add a background picture

Next, we'll create a new subdirectory for the picture. Create the images directory under the polls/static/polls directory. Then put the background picture Background.gif. In other words, the background picture path is polls/static/polls/images/background.gif.

Then, in the style sheet file (polls/static/polls/style.css), add:

/* POLLS/STATIC/POLLS/STYLE.CSS * *
    background:white url ("images/background.gif") No-repeat right Bottom;
}

Reload http://localhost:8000/polls/ you will see that the background image is placed in the lower-right corner of the page.

Warning

Obviously {% static%} template tags are not available in static files, such as style sheet files, because they are not Django generated. In static files, you should use a relative path (relative paths) to refer to each other. Because of this, you can change the static file address by changing static_url(thestatic module uses it to generate URLs) without having to modify the address of the referenced file in a large number of files.

These are just the Basics (Basics). Refer to the static files howto and the Staticfiles reference for a variety of settings for statically file, and for the rest and static files in the framework. In addition, deploying static files discusses ways to organize a static file on a real server.

If you understand static files, read the seventh part of the tutorial (en) to learn how to customize Django's automatically generated management site.

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.