django-basic two (url and view)

One, view

1. View function: complete the relevant logic
2. The view is written in the views.py folder of the app, and the first parameter of the view function must be request, and renturn must return the httpresponsebase object or a subclass object

Two, url

1. URL mapping

The role of url: the bridge between the view and the browser interface. When the user enters a url and requests to our website, django will look for the corresponding view from the project’s urls.py file

2. Add parameters to the URL

There are two main forms of URL adding parameters:
a. URL adding location parameters, this parameter needs to add the second parameter book_id in the corresponding views, which is convenient for receiving the parameter
url passed by the URL: http://127.0.0.1:7000/ index/2/
b, URL dictionary form return
url: http://127.0.0.1:7000/index_id/?id=2
Insert picture description here

3. Modular URL

Add the urls submodule in the respective app. At that time, the urls submodule can be added to the urls main module (via include) to facilitate clear mapping and solve problems caused by consistent names.

Insert picture description here

4. redirect and reverse

redirect: redirect to the internal URL
reverse: return the corresponding URL by the view name

django: Problem handling.
Solution: Add open(encoding="utf-8") to the debug.py file and open it in utf-8 format.
Insert picture description here

Redirect and reverse are used for redirection. When the ID number is entered in the URL, book_index2 is displayed, otherwise it is redirected to book_indexInsert picture description here

Guess you like

Origin blog.csdn.net/qq_37697566/article/details/106983363