是否有像通用视图一样的django中的通用模板

时间:2021-11-01 21:20:49

Generic view have saved lot of code for me but i still have to write templates of every model. I have same code in all template i.e

通用视图为我节省了大量代码,但我仍然需要编写每个模型的模板。我在所有模板中都有相同的代码,即

<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>

i.e basically i want to have all fields from the model to add or edit.

即基本上我想要从模型中添加或编辑所有字段。

is there any work around to have generic template automatrically

是否有任何工作可以自动生成通用模板

3 个解决方案

#1


1  

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

如果您的模板代码相同,则可以使用include标记:

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

And the included code can be modified with 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 having different fields, not sure), you can still use includes - just make 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 basic template, and have all your other templates inherit from it. Inheritance is block-based, you can override blocks in the parent template with new code in the child template. It works very well.

还有模板继承,您可以在其中定义基本模板,并从中继承所有其他模板。继承是基于块的,您可以使用子模板中的新代码覆盖父模板中的块。它工作得很好。

#2


0  

In django, templates can be generic itself!!

在django中,模板可以是通用的!

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

您可以使用{{form.attribute}}在同一模板中为每个模型使用不同的表单

Here is the django oficial doc

这是django oficial doc

#3


0  

Look at the ModelForm helper app. It will make a form from any model which can then be used in a simple form template.

查看ModelForm帮助应用程序。它将从任何模型创建一个表单,然后可以在一个简单的表单模板中使用。

#1


1  

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

如果您的模板代码相同,则可以使用include标记:

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

And the included code can be modified with 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 having different fields, not sure), you can still use includes - just make 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 basic template, and have all your other templates inherit from it. Inheritance is block-based, you can override blocks in the parent template with new code in the child template. It works very well.

还有模板继承,您可以在其中定义基本模板,并从中继承所有其他模板。继承是基于块的,您可以使用子模板中的新代码覆盖父模板中的块。它工作得很好。

#2


0  

In django, templates can be generic itself!!

在django中,模板可以是通用的!

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

您可以使用{{form.attribute}}在同一模板中为每个模型使用不同的表单

Here is the django oficial doc

这是django oficial doc

#3


0  

Look at the ModelForm helper app. It will make a form from any model which can then be used in a simple form template.

查看ModelForm帮助应用程序。它将从任何模型创建一个表单,然后可以在一个简单的表单模板中使用。