Vaheed01
Vaheed01

Reputation: 1057

Show model property if model exists and some string if not

To make it clear I want do the following with just one line:

 {%if model %}
      <input type="text" class="form-control" id="title" value="{{model.title}}" placeholder="Enter Title">
 {% else %}
      <input type="text" class="form-control" id="title" value="" placeholder="Enter Title">
 {% endif %}

I tried this:

 <input type="text" class="form-control" id="title" value="{% model.title if model else "" %}" >

And it didn't work:

Invalid block tag on line 15

I don't think I have to make a custom template tag for this simple kinda things.

Thanks in advance

Upvotes: 1

Views: 59

Answers (1)

Ahmed Ablak
Ahmed Ablak

Reputation: 738

You may try the following:

<input type="text" class="form-control" id="title" value="{%if model %} {{ model.title }}{% endif %}" placeholder="Enter Title">

Upvotes: 2

Related Questions