Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to add conditionals in html attribute

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading