Life is not all roses
Life is not a good road
The template in Django is the front-end display, or the HTML page. I don’t have much to introduce about the html, js, and css parts, because it is mainly the front-end stuff. This article mainly introduces the mutual value of the template and the view, as well as the inheritance of the template.
By default, we use render to render the index.html page in the view function.
Then create a new template file in the application directory, and create a new index.html file.
This part is relatively simple, such as using form form, passing some parameters to a specific url, of course, you need to set the urls.py route, otherwise the template request can not be accurately conveyed to the specific processing function of the view.
The template passes the default value parameters to the view:
Description: The update method passed to the view by the template. The parameter is nid and the value is i.nid.
Change the above view code to:
Description: In addition to strings, you can also pass data structures such as dictionaries and lists.
The value of the dictionary in python is list, which is used in the template.
The value of the dictionary in python is dict[“key”], which is used in the template.
Traverse the list:
Traverse the list and add the value after the output,
Traversing the dictionary:
The parameters of the loop:
Determine if the list is empty:
Determine if the element is in the list:
Generally, developing web pages requires writing some template pages, such as navigation bar, bottom copyright, side navigation, etc., or some function code. The iframe framework may have been used before, but it has now been eliminated. In order to avoid repeated code writing, and for the convenience of later modification, you can use the template inheritance method. The so-called template inheritance is to write a common template first, and then mark some variables. After the other pages are inherited, the mark can be modified by itself. If the template is not modified, the content of the template page is used.
Note: You can define as many block blocks as possible so that you can customize more places and use them more flexibly.
Environment: There is a root template first, then create a sub-template to inherit the root template, and then other pages inherit the sub-template.
Base.html (parent template page)
Base_ch.html (sub-template page)
Index.html (general inheritance page)