Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 590
Mirage Avatar asked Jun 28 '11 12:06

Mirage


1 Answers

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.

like image 76
John C Avatar answered Nov 07 '22 08:11

John C