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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With