Django fragmentation (1): simple interpretation of URLs

Source: Internet
Author: User
Tags file url

For specific url knowledge, see the django official website document or django book.

Here we will look at the following section urls. py:

# Coding = utf-8from django. conf. urls. defaults import * from django. conf import settings # access to any static files in the url path/site_media/,/theme_media/, And/upload_media/will be processed by django's built-in server function urlpatterns = patterns ('', (R' ^ site_media /(? P <path>. *) $ ', 'django. views. static. serve ', {'document _ root': settings. MEDIA_SITE}), (R' ^ theme_media /(? P <path>. *) $ ', 'django. views. static. serve ', {'document _ root': settings. THEME_SITE}), (R' ^ upload_media /(? P <path>. *) $ ', 'django. views. static. serve ', {'document _ root': settings. MEDIA_ROOT}),) # if not settings. DEBUG: urlpatterns + = patterns ('', url (R' ^ 404/$ ', 'django. views. generic. simple. direct_to_template ', {'template': '404.html'}), url (R' ^ 500/$ ', 'django. views. generic. simple. direct_to_template ', {'template': '404.html'}), url (R' ^ 505/$ ', 'django. views. generic. simple. direct_to_template ', {'template': '404.html '}),)
# Import urlpatterns + = patterns ('', (r'', include ('site1. urls '), # import (r'', include ('site2. urls '), # import)

I. Processing static files

Django's built-in Web Server cannot automatically process css/js/images/flw .. to access these static files, you must use the built-in server function of django to process access requests to various static files. add a static File url access entry to the patterns function of py :...... django. views. static. server

The server function accepts two parameters: one is the path parameter used to obtain the static file name of the request, and the other is the document_root parameter that specifies the actual static file storage path. The value of this parameter is specified by the following variable, as shown above: settings. MEDIA_SITE, settings. THEME_SITE, settings. UPLOAD_SITE. Because we have imported setting, these variables are stored in settings. configure in py as follows:

1 settings. py2 --------------------------------------------------------------- 3 DIRNAME = OS. path. dirname (_ file _) 4 # static file path in media (css, js, image ,..) 5 MEDIA_SITE = OS. path. join (DIRNAME, 'Media/') 6. static files (index.html, a.html ,...) 7 THEMES_SITE = OS. path. join (DIRNAME, 'themes/') 8 # upload path of the upload File 9 UPLOAD_SITE = OS. path. join (DIRNAME, 'uploads /')

Ii. custom 404 and 500 pages

1. Set settings to debug = false; 2. Set static Path 3. STATICFILES_DIRS = (PROJECT_DIR + '/static',) 4. Set urlsurl (R' ^ (? P <path>. *) $ ', 'django. views. static. serv', {'document _ root': settings.static_root=}5.put the predefined HTML and HTML under the root template. 6. Enter an incorrect address to display your custom error page. Z Note: If you load the css style of the admin page, you need to put all the admin css styles under the static directory.

Of course, the above is based on the non-debug mode

3. Configure multi-site urls

The application is based on multiple django sites and should take into account the URLConfig method containing [include] to process urls.

Read: http://blog.csdn.net/kevin6216/article/details/6913005

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.