开发者

Is there anything like Generic templates in django like Generic views

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 tem开发者_C百科plate 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


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

{% 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.


In django, templates can be generic itself!!

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

Here is the django oficial doc


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜