how to add conditionals in html attribute

Question:

I am using django 4.1.4 and I am new to it.

In a form I want to set the correct url based on the variable ‘update’. If the variable is True, then the template is used for update (in this case a different url), else save new data.

I created something like this:

<form action="{% if update %} {% url 'in.edit_session' session.id %} {% else %} {% url 'in.save_session' %} {% endif %}" method="POST" class="form">

but is not working.

This will work… but I don’t want to duplicate code:

  {% if update %}       
        <form action="{% url 'in.edit_session' session.id %}" method="POST" class="form">
    {% else %}       
        <form action="{% url 'in.save_session' %}" method="POST" class="form">
    {% endif %}

How can I do this ?

Asked By: calin24

||

Answers:

Delete spaces between brackets. It is html after all.

<form action="{% if update %}{% url 'in.edit_session' session.id %}{% else %}{% url 'in.save_session' %}{% endif %}" method="POST" class="form">
Answered By: NixonSparrow
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.