The URL of the Django framework detailed

Source: Internet
Author: User

Django Basic command Download Django
PIP3 Install Django
Create a Django Project
django-admin.py Startproject CMS

The current directory will generate a CMS project, the directory structure is as follows:

setting.py---Contains the default settings for the project, including database information, debug flags, and other work variables

urls.py---is responsible for mapping the URL pattern to the application

wsgi.py---for development server and Production WSGI deployment

manage.py---A tool inside the Django project that can invoke the Django shell and database, etc.

Create an App

Create an app under the CMS directory

Python manage.py Startapp App01

Start a Django Project
Python manage.py Runserver 8000

Synchronizing changes to a database table or field
Python manage.py Makemigrationspython manage.py Migrate

This way you can create a table, and when you add a class to the modles.py, you can run it to automatically create a table in the database

See all the commands

See all commands, forget the sub-name when it is useful

Python manage.py
Static file Configuration
' /static/ '  = (    'static'),)
The URL routing configuration system of the MTV model regular string parameters

Simple configuration

 fromDjango.conf.urlsImportURL fromDjango.contribImportAdmin fromApp01.viewsImport*Urlpatterns=[url (r'^admin/', admin.site.urls), url (r'^my_day/([0-9]{4})/$', my_day), url (r'^my_day/([0-9]{4})/([0-9]{2})/$', my_day2), url (r'^my_day/([0-9]{4})/([0-9]{2})/([0-9]{2})/$', My_day3),]#once the match is successful, it will not continue#To capture a value from a URL, simply place a pair of parentheses around it#There is no need to add a leading backslash, because each URL has (^my_time instead of ^/my_time)#The R in front of each regular expression is optional, but it is recommended to add
def My_day (request,year):     return HttpResponse (year) def My_day2 (request,year,month):     return HttpResponse ('%s-%s'%(year,month))def  My_day3 (Request , year,month,day):    return httpresponse ('%s-%s-%s'% (year, Month,day))
Code
# sets whether the item is open after the URL access address is not/jumps to the path with/ Append_slash=true
View Code

Named group (named Group)

The above example uses a regular expression group that is not named, and you can use named regular expression groups in more advanced usages, in a python regular expression, the syntax for a named regular expression group is (?). P<name>pattern)

 fromDjango.conf.urlsImportURL fromApp01.viewsImport*Urlpatterns=[url (r'^admin/', admin.site.urls), url (r'^my_day/(? P<YEAR>[0-9]{4})/$', my_day), url (r'^my_day/(? P<YEAR>[0-9]{4})/(? P<MONTH>[0-9]{2})/$', my_day2), url (r'^my_day/(? P<YEAR>[0-9]{4})/(? P<MONTH>[0-9]{2})/(? P<DAY>[0-9]{2})/$', My_day3),]#the captured value is passed to the view function as a keyword parameter instead of as a positional parameter

Urlconf Find

Urlconf looks up at the requested URL and treats it as a normal Python string, excluding the get and post parameters and the domain name

http://www.example.com/myapp/request, URLconf will look for myapp/. http://www.example.com/myapp/?page=3 in the request, URLconf will still find myapp/.
Example

Urlconf captured parameters are always strings

Each captured parameter is passed to the view as a normal Python string

Specify default values for view parameters

 from Import URL  from Import *= [    url (r'^my_day/(? P<YEAR>[0-9]{4})/$', my_day2),    URL (r'^my_day/(? P<YEAR>[0-9]{4})/(? P<MONTH>[0-9]{2})/$', My_day2),]
urlconf
def my_day2 (request,year,month='3'):    return HttpResponse ('%s-%s'% (year,month))
View

Two URLs all point to the same view, and if the first pattern matches, the view function will use the default value of the parameter, and if the second pattern matches, the view function will use the value captured by the regular expression

including other Urlconfs

 from Import URL  from Import Admin  from Import  = [    url (r'^admin/', admin.site.urls),    URL (r  '^app01', include ('app01.urls')] 
Passing additional options to the View function

Urlconfs has a hook that allows you to pass a Python dictionary as an additional parameter to the View function

 fromDjango.conf.urlsImportURL fromDjango.contribImportAdmin fromApp01.viewsImport*Urlpatterns=[url (r'^admin/', admin.site.urls), url (r'^my_day/(? P<YEAR>[0-9]{4})/$', My_day2, {'Foo':'Bar'}),]#The Django.conf.urls.url () function can receive an optional third parameter, which is a dictionary that represents the extra keyword arguments that you want to pass to the view function
URL Reverse resolution

In templates, Django uses URL template tags to reverse-parse URLs

 from Import URL  from Import *= [    url (r'^admin/', Admin.site.urls),    URL (r'^hello', My_hello, name='little_hello ' ),]
<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge">    <Metaname= "Viewport"content= "Width=device-width, initial-scale=1">    <title>My_day</title></Head><Body><ahref= "{%url ' Little_hello '%}">Point me to jump</a></Body></HTML>  

The URL of the Django framework detailed

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.