There is something like common patterns in django, such as general views

The general look has saved a lot of code for me, but I still have to write templates for each model. I have the same code throughout the ie template

<form action="/{{type}}/{{ action }}/" method="post" enctype="multipart/form-data" > {% csrf_token %} {% for field in form %} <div class="fieldWrapper"> {{ field.errors }} {{ field.label_tag }}: {{ field }} </div> {% endfor %} <p><input type="submit" value="Submit" /></p> </form> 

Basically I want all fields from the model to be added or edited.

is there any work to automatically create a common template

+6
source share
3 answers

If you have template code that is identical, you can use include tag :

 {% include "foo/bar.html" %} 

And the included code can be changed using variables:

 {% include "name_snippet.html" with person="Jane" %} 

Even if the code is different for each template (I think your example is talking about forms with different fields, not sure), you can still use include - just do two blocks:

 {% include "startform.html with some_action="post" %} {{ field.errors }} {{ field.label_tag }}: {{ field }} {{ field.field2_tag }}: {{ field2 }} {% include "endform.html %} 

There is also template inheritance where you can define a base template and inherit all other templates. Block-based inheritance, you can redefine blocks in the parent template with the new code in the child template. It works very well.

+1
source

In django, templates can be on their own!

You can use a different form for each model within the same template using {{form.attribute}}

Here is the django oficial doc

0
source

Check out the ModelForm helper application . It will make a form from any model, which can then be used in a simple form template.

0
source

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


All Articles