Static file path configuration for Django 1.4 and 1.3

高洛峰
Release: 2016-10-17 14:45:10
Original
1038 people have browsed it

There are two files that need to be modified for routing configuration (static file path configuration) in Django:
Generally we will put all static files in a folder, so now we need to create a new static directory in the project directory , and then place all the static files needed for the website: CSS, pictures, JS, etc. in the static directory.

Note: The configuration methods of Django 1.4 and 1.3 are slightly different.

Static file path configuration

1 in Django 1.4, add in urls.py:


url(r'^static/(?P. *)$', 'django.views.static.serve'),
Django’s own static file server is used here for processing. It is no longer needed after it is deployed 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 talk about the static file path configuration of Django1.3:

1. Set the static file path

We need to set it in the settings.py file Set a static file path STATIC_PATH, which is the directory where we just stored the static files. In order to avoid hard-coding the path, 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')
We need to use this STATIC_PATH 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, we can use other names for the "static" in the urlconf. By convention, we generally use "static"



One last thing to note: everything is configured, now if we want to use static files in template files, we can call them normally. Please note that "/" must be added at the beginning of the path



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template