Home > Software design >  Show model property if model exists and some string if not
Show model property if model exists and some string if not

Time:09-30

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

CodePudding user response:

You may try the following:

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