Home  >  Article  >  Backend Development  >  "URL mapping rules" and "server response order" in Django

"URL mapping rules" and "server response order" in Django

高洛峰
高洛峰Original
2016-10-17 13:55:251114browse

1. Django search path

  When using the import statement, the list of system directories that Python searches for.

View method:

Import sys

Print sys.path

Usually you don’t need to care about the setting of Python search path, Python and Django will automatically handle it for you in the background.

2. URL matching mode

Basic structure:

'^The url string that needs to be matched$'

PS: In fact, the final complete url string is http://root path: port number/url that needs to be matched String

The part automatically added by the system is 'http://root path: port number/'

eg: URL matching pattern: '^latest_books/$'

The final complete url string: 'http://127.0 .0.1:8000/latest_books/'

1) ^: Matches "substring header".

  eg:

'^latest_books/'

'http://127.0.0.1:8000/latest_books/',

'http://127.0.0.1:8000/latest_books/test1/',

' http://127.0.0.1:8000/latest_books/test2/',

'http://127.0.0.1:8000/latest_books/test1/aaa/'

will be matched. ​​

​ 2)$: Matches "the end of the substring".

eg:

'latest_books/$'

'http://127.0.0.1:8000/latest_books/',

'http://127.0.0.1:8000/updir_1/latest_books/',

' http://127.0.0.1:8000/updir_2/latest_books/'

will be matched.

3) Whether the end of the substring contains '/'

It must be added by default (the basic habit of django developers). If not added, the following situation will occur:

from django.conf.urls import patterns, url, include

urlpatterns = patterns('',

(r'^latest_books$', 'django_web_app.views.latest_books'),

)

URL mapping rules and server response order in Django

If you don’t want to include '/' at the end of the substring, you can set it Add settings in .py: APPEND_SLASH=False

But CommonMiddleware must be installed for it to work.

4 Function":

  a)urls.py

from django.conf.urls import patterns, url, include

URL mapping rules and server response order in Djangourlpatterns = patterns('',

                            (r'^$','django_web_app.views.home_page') ) _response('home_page.html')

   c)home_page.html

my home page

This is home page , welcome!

 running results :

Attachment: Basics of regular expressions

3. Execution order of server response to url requests

1) Project structure

django_web

  __init__.py

   settings.py

URL mapping rules and server response order in Django    urls .py

   wsgi.py

 django_web_app

   __init__.py

    admin.py

   models.py

   tests.py

   views.py

 templates

   home_page.html

   latest_books.html

manage.py

 2) Execution sequence

 a) Start the server - python manage.py runserver

  Get the configuration in the setting.py file, mainly including:

  URL mapping relationship file path:

ROOT_URLCONF = ' django_web.urls'

   Page file template path:

TEMPLATE_DIRS = (

os.path.join(BASE_DIR, 'templates'),

)

    Database configuration:

DATABASES = {

  'default': {

   'ENGINE': 'django.db.backends.mysql',

  'NAME': 'django_db',

'USER': 'root ',

'PASSWORD': 'feng',

'HOST': '127.0.0.1',

'PORT': '3306',

}

}

b) Response sequence

First Step 1: The browser submits the request

  http://127.0.0.1:8000/latest_books/

Step 2: The server matches the requested URL in urls.py and finds the corresponding "view function"

Step 3: Call the corresponding "view function" to return an HttpResponse object

Step 4: Django converts the HttpResponse object into a suitable HTTP response and returns it to the page for display


Statement:
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
Previous article:Django unit testing (1)Next article:Django unit testing (1)