Home > Back-end >  how to add conditionals in html attribute
how to add conditionals in html attribute

Time:01-04

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

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" >
    {% else %}       
        <form action="{% url 'in.save_session' %}" method="POST" >
    {% endif %}

How can I do this ?

CodePudding user response:

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" >
  • Related