Static file path configuration for Django1.4 and 1.3

Source: Internet
Author: User
In Django, there are two files to modify for the routing configuration (static file path configuration):
Generally we will put all the static files in a folder, so now we want to create a new static directory under the project directory, and then the site needs to use the static files: CSS, pictures, JS and so on all in the static directory.

Note: The Django1.4 and 1.3 configuration methods are small and different

Static file path configuration in Django1.4

Added in 1,urls.py:


URL (r ' ^static/(? P . *) $ ', ' django.views.static.serve '),
This is handled using Django's own static file server, which is not needed 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 ')
)


Let's take a look at the static file path configuration for Django1.3:

1. Set the static file path

We want to set a static file path Static_path in the settings.py file, which is the directory where we just stored the static file. To avoid writing the path, we can use some of the methods in the OS module to convert the absolute path, adding 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 we are going to use when configuring URLs.

2. Configure static file URLs

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

From django.conf Import settings
URL (r ' ^static/(? P . *) $ ', ' Django.views.static.serve ', {' Document_root ': Settings. Static_path},name= "Media")

Of course the urlconf in the "static" we can use other names, according to the convention we generally use "static"



Finally, one thing to note: And now if we want to use the static file in the template file, we can call it normally. Be aware that the beginning of the path is prefixed with "/"
  • 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.