[Answer]-Reuse custom form template in django

1👍

You have to {% extend "form.html" %} in order to override those blocks. Blocks are overriden only for the template in the extend declarations.

A solution would be to create a new template for the new form that extends the old one:

{% extend "base.html" %}
{% include "my_form.html" %}

# my_form.html
{% extend 'form.html' %}
{% block formtitle %}Create a new MyObject{% endblock %}
{% block formbutton %}Create{% endblock %}

Or pass variables to the include statement:

{% extend "base.html" %}
{% include "form.html" with formtitle='Create new MyObject' formbutton="My button title" %}


# form.html
<h1>{% block formtitle %}{{ formtitle }}{% endblock %}</h1>
<form method="post" action="">
  {% csrf_token %}

  [stuff]
  <button type="submit">{% block formbutton %}{{ formbutton }}{% endblock %}</button>
</form>

Leave a comment