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'),
)
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.pyfrom django.conf.urls import patterns, url, includeurlpatterns = patterns('',
(r'^$','django_web_app.views.home_page') ) _response('home_page.html')
c)home_page.html
my home page
This is home page , welcome!
Attachment: Basics of regular expressions
django_web
__init__.py settings.pyurls .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