Show model property if model exists and some string if not - django

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

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

Related

I am manually generating a form in django(3.1.2) framework, but the name and type field are empty while rendering form

<div class="col-sm-9">
<input type="{{ widget.type }}" class="form-control mt-3" id="{{form.Address.id_for_label}}" name="{{ widget.name }}" {% if widget.value != None %} value="{{ widget.value|stringformat:'s' }}"{% endif %} {% include "django/forms/widgets/attrs.html" %} />
</div>
name and type field are empty. so i cannot pass the form data to request.POST.
<div class="col-sm-9">
<input type="" class="form-control mt-3" id="id_Address" name="" />
</div>

How can I access the outer loop context value in inner loop section in Django Template

In my case, I want to access the context value which is present in the outer for loop and use(print) that value in the inner for loop section. There are two context values one for the outer loop and the other one is for the inner loop.
here is my template logic. when the condition becomes false it did not print the values that belong to the outer-loop context variable(else part of if statement not working).
<div class="panel-group" id="accordion">
{% for x in queryset %}<!--outerloop-->
<div id="collapse{{ forloop.counter }}" class="panel-collapse collapse">
<div class="panel-body">
<div>
<form action="tobeupdated" method="POST" enctype="multipart/form-data" >
{% csrf_token %}
<label>Q- <input type="text" id="ques" name="ques" value="{{x.question.question}}" readonly></label><br><br>
<img src="{{x.question.image}}" alt="" srcset="" width="700"><br><br>
{% for q2 in queryset2 %}<!--innerloop-->
{% if q2.option_id == x.option_id %}
<label>A- <input type="text" id="op1" name="op1" value="{{q2.option.option1}}" readonly><br><br>
<img src="{{q2.option1_img}}" alt="" srcset="" width="400"><br><br>
<label>B- <input type="text" id="op2" name="op2" value="{{q2.option.option2}}" readonly><br><br>
<img src="{{q2.option2_img}}" alt="" srcset="" width="400"><br><br>
<label>C- <input type="text" id="op3" name="op3" value="{{q2.option.option3}}" readonly><br><br>
<img src="{{q2.option3_img}}" alt="" srcset="" width="400"><br><br>
<label>D- <input type="text" id="op4" name="op4" value="{{q2.option.option4}}" readonly><br><br>
<img src="{{q2.option4_img}}" alt="" srcset="" width="400"><br><br>
{% else %}
<label>A- <input type="text" id="op1" name="op1" value="{{x.option.option1}}" readonly><br><br>
<label>B- <input type="text" id="op2" name="op2" value="{{x.option.option2}}" readonly><br><br>
<label>C- <input type="text" id="op3" name="op3" value="{{x.option.option3}}" readonly><br><br>
<label>D- <input type="text" id="op4" name="op4" value="{{x.option.option4}}" readonly><br><br>
{% endif %}
{% endfor %}
<label>Correct Ans- <input type="text" id="ans" name="ans" value="{{x.correct_ans}}" readonly><br><br>
<input type="hidden" id="dlevel" name="dlevel" value="{{x.question.difficulty}}">
<input type="checkbox" name="" checked>
<label>{{x.question.difficulty}}</label>
<!-- <input type="submit" id="update" value="Update" /> -->
<button type="submit" class="btn btn-primary" name="qid" value="{{x.question.id}}" >Edit</button>
<button type="submit" class="btn btn-primary" name="delete_qid" value="{{x.question.id}}" >Delete</button>
</form>
</div>
<!--Toogle Button-->
</div>
</div>
{{x.question.id}}
<!-- Button trigger modal -->
</div>
</div>
{% endfor %}
</div>
Any suggestions regarding this problem will be highly appreciated.

Django default value in in input field

I need to set a placeholder/default value for the input field below (the first field in the form)
I need to send the data ex_name.id in order to validate the form, which it does, but I want the information displayed in the field to be ex_name.exercise.
I was able to accomplish this in the option fields (fields 2 and 3), but not for the input field. If I make the input field an option field, it doesn't send the data when I submit it because the field is readonly and it has to stay readonly. value must be ex_name.id/ex_name.pk, but I don't want to display a number to the end user. How to I display different text there while still retaining value="{{ex_name.id}}"?
<div class="form-group col-lg-4 col-md-6 col-sm-12">
<form action="" method="post">
{% csrf_token %}
<label for="exercise">Exercise</label>
<input class="form-control" id="id_exercise" type="text" name="exercise" value="{{ex_name.id}}" required="" readonly>
<label for="unit">Select Unit</label>
<select class="form-control" id="id_unit" name="unit" value="">
{% for unit in unit_list %}
<option value="{{unit.id}}">{{unit.unit_name}}</option>
{% endfor %}
</select>
<label for="location">Location</label>
<select class="form-control" id="id_location" name="location" value="">
{% for loc in locations %}
<option value="{{loc.id}}">{{loc.location}}</option>
{% endfor %}
</select>
<label for="quantity">Pax</label>
<input class="form-control" id="id_quantity" type="text" name="quantity" value="" required="">
<input class="btn btn-primary mt-3" type="submit" value="Submit">
</form>
</div>

How can I convert a field in list in django template?

please anyone can help me ? How can I convert a field string to list ? I have a field called Size, it means that the field contains somthing as : "XXL, XL, X, SM".
So I want to get something as :
<form class="form-control" action="index.html" method="post">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="taille">
<label class="custom-control-label" for="taille">SM</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="size">
<label class="custom-control-label" for="size">XXl</label>
</div>
<div class="customenter code here-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="size">
<label class="custom-control-label" for="size">XL</label>
</div>
</form>
What's your recommandations guys ?
Thank you guys, I already got the answer. I solved it by creating a table with the field article_size in models.py file.
Here is the working code:
<form class="form-control" action="index.html" method="post">
{% for size in article.size_article.all %}
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="taille{{size.article_size}}">
<label class="custom-control-label">{{size.article_size}}</label>
</div>
{% endfor %}
</form>

Django allauth override default template with custom template

I'm currently trying to override a Django allauth template. So far, I followed all the recommendations on similar questions to mine:
overriding default templates of django-allauth
How to override template in django-allauth?
However, my goal is just change the default Django allauth without making new views or forms. (In previous projects I used to override the default forms constructor classes with the helper of Django Crispy Forms which is not possible in this particular case.)
I believe there is a way to ignore the {{form}} variable and just define a working form from scratch. Currently, I have my template being successfully being displayed when I navigate to 'account_login'. However, when I press Sign In nothing happens.
Here is my myapp/templates/account/login.html file:
{% extends 'base.html' %}
{% load staticfiles %}
{% block title %}
Sign In to {{ SITE_NAME }}
{% endblock %}
{% block body %}
<div class="centered">
<img class="logo" src={% static 'imgs/logo.png' %} alt="" width="100" height="100">
<form id="login_form" class="form-signin" method="post" action="{% url 'account_login'%}">
{% csrf_token %}
<label for="inputLogin" class="sr-only">Email address or Username</label>
<input type="text" id="login" class="form-control" placeholder="Email or Username" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="password" class="form-control" placeholder="Password" required>
<div class="note mb-3">
<label>
<input type="checkbox" value="remember" id="remember"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<div class="note mb-3 text-center">
<p>Forgot? Retrieve your login.</p>
<p>Not registered? <a href={% url 'account_signup'%}>Sign Up!</a></p>
</div>
</form>
</div>
{% endblock %}
Yes, you can customize it the way you want. It is rather simple.
When you use {{ form.as_p }} the page renders the form. And if you inspect the form you will notice that the page rendered the following form:
notice the id="" and the name="" you have to name it the exact same.
With that your code should look like this:
<form id="login_form" class="form-signin" method="post" action="{% url 'account_login'%}">
{% csrf_token %}
<label for="inputLogin" class="sr-only">Email address or Username</label>
<input type="text" name="login" id="id_login" class="form-control" placeholder="Email or Username" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="id_password" class="form-control" placeholder="Password" required>
<div class="note mb-3">
<label>
<input type="checkbox" name="remember" value="remember" id="id_remember"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<div class="note mb-3 text-center">
<p>Forgot? Retrieve your login.</p>
<p>Not registered? <a href={% url 'account_signup'%}>Sign Up!</a></p>
</div>
</form>
And you should be good to go.