Question

I write a template for my form like

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

[stuff]

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

now i want to use the same template both for the creation and for the edit of an object, but how?

i tried to create a template for the creation like

{% extend "base.html" %}
{% include "form.html" %}
{% block formtitle %}Create a new MyObject{% endblock %}
{% block formbutton %}Create{% endblock %}

but i got a blank page...

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top