Django Config urlconf and get values

Source: Internet
Author: User

Django correctly configures the URL match to find the view:

1 in settings.py under Project root_urlconf = "project name. URLs"

Indicates that the foreground sends a request to the test3/urls.py file under the project to make a regular match to find the view function that should be executed.

2 Configuring in the test3/urls.py file Urlpatterns

The incoming request will enter the Urlpatters list to match the regular expression sequentially, matching the successful entry URL () to the second parameter specified

Send to request if IP: Port/aaa/bbb/?a=23 is similar to this format, to match here will be the Django split left aaa/bbb/to match

The first include (Admin.site.urls) represents the Django Background Database Administration page

After the first one, we developed the match in our own business.

The second one is a request, and if it is 127.0.0.1:8000/demo/xxx/xxx/, there will be demo/xxx/xxx/to match, and the second match will be successful.

After the match succeeds, it will be deleted to match the success of the part left XXX/XXX to go to the URL of the second parameter include ("Demo1.ulrs") to match,

Refers to the urls.py continuation regular match under the DEMO1 application module

The third is that any match to this place can succeed, and then turn to the urls.py continuation regular match under the Booktest application

3 booktest references under urls.py file settings match:

Jump to the application's urls.py from the urls.py match of the previous project, and enter the urlpatters in turn to match

Here, a successful match with the regular will go to the function specified by the second parameter:

If the remaining string is index/, it will go to views.py to find the index function.

If the rest is add/, it will go to views.py to find the Add function and execute

 

4 views.py in view functions

In views.py, the definition of a function name is consistent with the result of a successful match.

The matching request executes the function, completes some business logic, and returns response.

The request to the browser will receive this response

Get Value:

In the way of URL matching, there are two ways to pass parameters to the Views view function: Positional parameter, keyword parameter

1 Position Parameters:

Use parentheses to group regular expressions and pass them to the view by positional parameters.

1) in order to extract the parameters, modify the above regular expression as follows:

URL (r'^delete (\d+)/$', Views.show_arg),

The Show_arg function in the views.py file then sets the parameters to receive the regular grouping

def Show_arg (request,id):     return HttpResponse ('show arg%s'%id)

2 keyword Parameters:

Name the group in the regular Expression section.

1) Modify the regular expression as follows:

which? The P section indicates that the name defined for this parameter is an ID, which can be a different name and is named to be known

URL (r'^delete (? p<id1>\d+)/$', Views.show_arg),

2) Modify the view Show_arg as follows:

Note: View show_arg at this point must have a parameter named ID1, otherwise error.

def Show_arg (REQUEST,ID1):     return HttpResponse ('show%s'%id1)

Django Config urlconf and get values

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.