Insert a block inside another in Django

I have a Django template that I want to expand in several places. In some, the div should be inside the form, while in others it should not. To do this, I put the block above and below the div so that I can add them to them accordingly.

Desired:

<form> <div class="my_div"> {% block div_content %} ... {% endblock %} </div> </form> 

Template:

 {% block div_top %}{% endblock %} <div class="my_div"> {% block div_content %} {% endblock %} </div> {% block div_bottom %}{% endblock %} 

Looking at this, I cannot help but think that there is a better way to do this. What is the standard way for Django to do this?

+4
source share
1 answer

Using multiple basic patterns is the solution I've seen in several teams. For example, you can simply add an additional base template called "base_with_form.html". Templates that need form extend this basic template.

One thing that helped me was to think about decomposing the template directories in the same way as Python packages. I usually use base.html for each directory (ala init .py), even if it's just a placeholder. Each base file extends the base file in the parent directory. Additional style specializations for several templates in the same directory are done by adding copies of the local base.html with the required changes.

Example:

 templates/ base.html index.html (extends "base.html") accounts/ base.html (extends "base.html") affiliate_base.html (extends "base.html") my_account.html (extends "accounts/base.html") affiliate_dashboard.html (extends "accounts/affiliate_base.html") vips/ base.html (extend "accounts/base.html") vip_lounge.html (extends "accounts/vips/base.html") 
+8
source

Source: https://habr.com/ru/post/1303996/


All Articles