GAE template code to check is item in the list - python

How to use "in" statement to check is item in the list or not. If I use:
{% for picture in pictures %}
{% if picture in article.pictures %}
<input type="checkbox" checked="true" name="picture" value="{{ picture.key }}" />
{% else %}
<input type="checkbox" name="picture" value="{{ picture.key }}" />
{% endif %}
<img src='/img?img_id={{ picture.key }}'></img> <br />
{% endfor %}
this is failing with:
TemplateSyntaxError: 'if' statement improperly formatted
on line
{% if picture in article.pictures %}
help?

By default, Django templates do not support full conditional expressions. You can check if one value is "true" with if, or you can check whether two values are equal with ifequal, etc.
Perhaps you can decorate your pictures in the view before you render the template.
for picture in pictures:
picture.is_in_article = (picture in article.pictures)
Then in the template you can act on the value of that new attribute.
{% for picture in pictures %}
{% if picture.is_in_article %}
<input type="checkbox" checked="true" name="picture" value="{{ picture.key }}" />
{% else %}
<input type="checkbox" name="picture" value="{{ picture.key }}" />
{% endif %}
<img src='/img?img_id={{ picture.key }}'></img> <br />
{% endfor %}

I've not worked with GAE, but the code for a custom "ifin" Django tag can be found in the patch here. As mentioned in my comment, it looks like that functionality may be implemented in Django 1.2

Related

HTML element to dynamically pop up specific suggestions for field in form

I'm working on a database tool using python+flask+werkzeug and learning how to use most of it for the first time. For the "Institution" field in the template, as the user starts typing in the name I would like existing options that fit the pattern to pop up in a drop-down, then with an option to add a new one if it's not found in that list. I've found the <select> element, and I can see a way to do it with that. However, it just has hard-coded in choices with no way to type in, and there may be hundreds of institutions I wouldn't want to put all in here. I don't know the terminology for the element I'm looking for so I don't really know what to google. Any thoughts?
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Add New Member{% endblock %}</h1>
{% endblock %}
{% block content %}
<form method="post">
<label for="full_name">Full Name</label>
<input type="text" name="full_name" id="full_name" value="{{ request.form['full_name'] }}" required>
<label for="phone_number">Phone #</label>
<input type="tel" name="phone_number" id="phone_number" value="{{ request.form['phone_number'] }}">
<label for="email">Email</label>
<input type="email" name="email" id="email" value="{{ request.form['email'] }}">
<label for="institution_name">Institution</label>
<select id="institution_name" name="institution_name">
{% for institution in institution %}
<option value="{{ institution['institution_name'] }}">{{ institution['institution_name'] }}</option>
{% endfor %}}
<option value="not_found">Not found. Add new?</option>
</select>
<input type="submit" value="Submit">
</form>
{% endblock %}
There are JavaScript plugins that allow for this, such as Select2, but a simpler option could be to use a datalist element for this.
<div>
<datalist id="institution_list">
{% for institution in institution %}
<option value="{{ institution['institution_name'] }}">{{ institution['institution_name'] }}</option>
{% endfor %}}
</datalist>
<input autoComplete="on" list="institution_list"/>
</div>

working with form in Django

This is page that designed to submit the answers of my MCQ(multiple choice question) App in Django.
{% extends 'quiz/base.html' %}
{% block content%}
<h1>You are at quiz page</h1>
<form action="{% url 'quiz:process_data' %}" method="post">
{% csrf_token %}
{% for question in question_set %}
<h3>{{question.id}}.{{question.question_text }}</h3>
{% for option in question.options_set.all %}
<input type="radio" name="choice" value="{{ option.options}}" > {{option.options}}<br>
{% endfor %}
{% endfor %}
<input type="Submit" name="Submit">
</form>
{% endblock%}
The problem is that it can only select the answer of one question. There is something wrong with my form. Thank you
<input type="radio" name="choice" value="{{ option.options}}">
by this line name of radio button will be same for each question. that's why you are able to select only one answer.
name of radio button should be different for diff. question
<input type="radio" name="choice{{question.id}}" value="{{ option.options}}">
you can use this code. In this name of radio buttons in first question will be "choices1". and you can get selected option by
request.POST.get('choice1')

Django Template to make a list to be sent as post

I am trying to make an MCQ quiz and I have used the following code in a template:
{% for MCQ in mcq %}
<input type="radio" name="MCQ" value="{{ MCQ.id }}">{{ MCQ.MCQ_Text }}
{% endfor %}
The problem is that I want to use MCQ_list = request.POST[MCQ] to access it as a list. How do I make a list in the template? Alternatively, is there a better way to approach this?
Edit:
The mcq is nested inside
{% for Questions, mcq in Quest_dic.items %}
<br>
{{ Questions.Question_Text }}
{% for MCQ in mcq %}
<br>
<input type="radio" name="ListOrAlternateHere" value="{{ MCQ.id }}">{{ MCQ.MCQ_Text }}
{% endfor %}
<hr>
{% endfor %}
change your input to type=checkbox. This will let you read the checked items as a list in python:
mcq_list = request.POST.getlist('MCQ')

Trying to create a button per object instance

I'm trying to make a list of buttons, each corresponding to an object in my database. However, when I press one of them, it is always the value of the last button generated which is sent back to the view:
{% if segmenter %}
<form method="post">
{% csrf_token %}
<ul>
{% for segment in segments %}
<li>
<input type="hidden" name="id" value="{{ segment.pk }}"/>
<button type="submit">{{ segment }}</button>
</li>
{% endfor %}
</ul>
{% else %}
<strong>No segments registered. </strong><br />
{% endif %}
Form items are distinguished by name but you're using the same name for your inputs. Besides using one form means you intend to send all the inputs when the inputs are differentiated.
You should do:
{% if segmenter %}
{% csrf_token %}
<ul>
{% for segment in segments %}
<form method="post">
<li>
<input type="hidden" name="id_{{ segment.pk }}" value="{{ segment.pk }}"/>
<button type="submit">{{ segment }}</button>
</li>
</form>
{% endfor %}
</ul>
{% else %}
<strong>No segments registered. </strong><br />
{% endif %}
which posts a separate form on click of each button

Display objects based on attribute

This is very simple. I have a database of objects with pin attributes.
use case
user inputs 4-digit pin
displays objects with matching pin number
What is the simplest way to accomplish this in django?
index.html
<form action="/polls/" method="post">
{% csrf_token %}
<p>
<label for="pin">Enter group pin:</label>
<input id="pin" type="text" name="pin" maxlength="4" />
<input type="submit" value="View Polls" />
</p>
</form>
Currently it's hard coded in
{% for poll in latest_poll_list %}
{% if poll.pin == "1234" %}
<ul class="poll-list">
<li>{{ poll.question }} - {{poll.pin}}</li>
</ul>
{% endif %}
{% endfor %}
I am pretty new to django so there may be a better solution, but I'll give it a try.
In your view you can do something like this. Considering your code I assume you have a class
Poll
poll = Poll.objects.filter(pin=request.POST['pin'])

Categories

Resources