In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "Django template inheritance and static resources how to use", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Django template inheritance and static resources how to use" article.
1. Template inheritance
For a website, there will be a lot of html files, but because some pages will have the same page structure. Also led to multiple html files in the code is duplicate, so in order to reduce repetitive code in html, simplify the html structure, multiple pages of the same html code can be extracted separately in a html file, other html files if you want to use this part of the content, directly inherit the past on it, a bit similar to the class inheritance relationship.
Inheritance relationship: parent template and child template.
2. Suppose there is a website with the home page, list page and details page. The navigation bar at the top of the page is the same as the toolbar at the bottom of the page. The only change is the content. A simple picture is drawn, which is roughly shown below:
For this site, the content above% 70 (boast ()? ") is similar, if every html file writes the same part of the code, it is OK, but it will cause code redundancy and so on. In order to solve this problem and reduce the occurrence of duplicate code, template inheritance solves this problem very well.
Now let's implement the inheritance of the template.
1. Create a project and app
Django-admin startproject djangotemplateinherit
Cd djangotemplateinherit
Python manage.py startapp teminherit
2. Create a templates folder under the root of the project.
3. Write the route in the urls.py file.
4. Write business logic, that is, view function.
5. Create 3 new html files under the templates folder.
4. Create a new nav.html that represents the common part of the navigation bar.
At the same time, we change the view function in the views.py file to render the template.
5. We write the following code in nav.html
6. Introduce the nav.html navigation bar template into index.html,list.html,detail.html respectively.
Template inheritance requires the use of the extends keyword.
Extends: keyword indicates the parent template inherited by the current file index.html
Inheriting inherits all the code for this file (the current case is the nav.html file).
Write the following code in the index.html file:
The other two list.html,detail.html files are written to the same content as index.html.
7. Run and view
At this point, all three html use a common navigation bar (nav.html file).
8, although the use of a common navigation bar, but each html file, we can not add the content of each page (that is, home page content, list page content, details page content), do not believe it, you can try? At this point, we write an h3 tag in index.html.
Refresh the page again and find that there is no content we want on the home page.
The reason is that we have fixed the content in nav.html. When extends inherits the template, it completely introduces the content of the nav.html file and replaces the content of index.html, so the h3 tag we wrote cannot be run. It is worth noting that if you put the h3 tag before the {% extends' nav.html'%} code, you can run the display. Don't try it here, please try it yourself.
9. What we need now is that the template file of index,list,detail will show different content after rendering. However, because of the three index,list,detail template files, we now inherit the content of nav.html, so it is impossible to add the code {% extends' nav.html'%} directly to the end, and lose the meaning of the public navigation bar before {% extends' nav.html'%}. So the only solution now is to change the nav.html so that it allows us to implement custom functionality in different template files.
Django provides us with a block tag. If the subtemplate needs to add some content of its own page based on the current page, use django's built-in template tag block (code block), which needs to be given a name. You can do whatever you want.
10. Now let's modify the nav.html to reserve the interface for the custom code. That is, add {% block content%} {% endblock%}.
At the same time, add a custom H2 tag to the index.html file.
Refresh again, and the index home page has changed. More than our custom H2 tag. The corresponding changes to the other two template files are the same as index.html, and no attempt will be made here.
11. Although the content of the home page of index is customized. But careful students will find a small defect.
That is, on the tab that opens the home page of index, the title field in nav.html is still displayed. We know that on sites such as Bole online, the title on the newly opened window changes every time you click on a module in the navigation bar. Now that we have inherited the template, the corresponding title content will be written dead. So what should we do with it?
12. In order to solve this problem, we modify the content of nav.html again, as shown in the following figure:
Note:
The label set in block is equivalent to the default value of a title.
If the subtemplate does not override the title block, the default value is used. If the subtemplate overrides the title block, the value of the subtemplate is used.
13. Since we need to change the title title of the home page of index, we need to change the content of index.html again. Modify the title we need through the interface reserved by block. This is similar to classes, where we can inherit classes and rewrite those that do not meet our needs at the same time.
The above is about the content of this article on "Django template inheritance and how to use static resources". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.