Static file path configuration for Django1.4 and 1.3

Source: Internet
Author: User
Static file path configuration in Django1.4 and 1.3 in django (static file path configuration), there are two files to be modified:
Generally, we will put all the static files in a folder, so now we need to create a static directory under the project directory, and then the static files required for the website: CSS, images, and JS are all stored in the static directory.

Note: the configuration methods for Django1.4 and 1.3 are slightly different.

Static file path configuration in Django1.4

1. add the following in urls. py:


Url (r' ^ static /(? P . *) $ ', 'Django. views. static. serv '),
Here, the static file server provided by django is used for processing. it is not required after deployment to the production environment.
2, settings. py

ROOT_PATH = OS. path. normpath (OS. path. dirname (_ file _). replace ('\\','/')
STATIC_URL = '/static /'
TEMPLATE_DIRS = (
OS. path. join (ROOT_PATH, '../templates ')
)


Next, let's talk about the static file path configuration of Django1.3:

1. set the static file path

Set a static file path STATIC_PATH in the settings. py file, that is, the directory where the static file is stored. To avoid writing the path to death, we can use some methods in the OS module to convert the absolute path. add the following code to the settings. py file:

Import OS
Your_path = lambda * x: OS. path. join (OS. path. abspath (OS. path. dirname (_ file _), * x)
STATIC_PATH = your_path ('static ')
This STATIC_PATH is used when urls is configured.

2. configure the static file urls

In the urls. py file, add the following code:

From django. conf import settings
Url (r' ^ static /(? P . *) $ ', 'Django. views. static. serv', {'document _ root': settings. STATIC_PATH}, name = "media ")

Of course, we can use other names for the "static" in urlconf. we generally use "static" as a convention"



Note: Everything is configured. now, if we want to use static files in the template file, we can call them normally. Note that "/" must be added at the beginning of the path.

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.