Display objects based on attribute - python

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'])

Related

How to style django-multiselectfield with CSS

I am using 'django-multiselectfield' in my model form so users can select multiple 'areas' in my form. What I want to know now is how can I style the multiselectfield using css.
I have tried simply adding a class to the form field in my template like I normally do with all the other types of model fields, but no luck:
{% render_field form.area class="area" %}
Any help would be much appreciated.
This is described in `django-multiselectfield' documentation. Specifically,
It is possible to customize the HTML of this widget in your form template. To do so, you will need to loop through form {field}.field.choices. Here is an example that displays the field label underneath/after the checkbox for a MultiSelectField called providers:
Here is a example of what i did.
{% for field in form %}
<div class="fieldWrapper">{{ field.errors }}
<input type="{{ field.field.widget.input_type }}" name="{{ field.html_name }}" id="{{field.id_for_label}}" class="col-12 input-login" placeholder="{{field.label}}" >
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}
Now you can customise the field as if it is a simple html input field.
This page has a wonderful detailed explanation. Kudos to Vitor
------edit-----
Almost forgot. This is the example for normal forms. You have to go through the official docs of django-multiselectfield to get field attribute names, and replace the respective attributes
I suggest you render the form manually and loop through it.
Say you have a Choice model with a foreignkey to a Question model, you could do it like this:
<form class="qform" action="" method="post">
{% csrf_token %}
<p class="title">{{ form.question }}</p>
{% for choice in form.choice_set.all %}
<input type="checkbox" name="{{ choice }}" id="{{ choice }}{{ forloop.counter }}"
value="{{ choice.id }}">
<label for="{{ choice }}{{ forloop.counter }}">{{ choice }}</label>
{% endfor %}
</form>
You can reference the input and label in your style.css like:
input[type="checkbox"]{
some: stuffs...;
}
label {
some: stuffs...;
}
I hope this helps.

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

How to manipulate contents of a Django template based on value of radio button elements (without JS)

In a Django template, I have two radio buttons. If the second of these radio buttons is selected, I want certain follow-up elements to show. How do I do this in the Django template?
Here's my code:
<form method="post" action="" method="POST">
{% csrf_token %}
Privacy:<br>
{% for radio in form.private %}
<table><tr><label for="{{ radio.id_for_label }}">
<td><span>{{ radio.choice_label }}</span>
<span class="radio">{{ radio.tag }}</span></td>
</label></tr></table>
{% endfor %}<br>
{% if radio.tag == 'value1' %}
{{ form.rules.errors }}
{{ form.rules.label_tag }}{{ form.rules }}<br>
{% endif %}
<br>
<input class="button" type="submit" value="OK">
</form>
Currently, I'm trying {% if radio.tag == 'value1' %} to try and accomplish what I posted in my question at the top, i.e., if the radio tag has a certain value, element called rules will display. Needless to say, it's not working for me. Any suggestions? I'd prefer not to use Javascript, because more than 80% of the users using this system have legacy phone devices that do not support JS.
Note: please ask me for any clarifications, where warranted. I may not have successfully provided all the information you need.
You need to compare as follows:
{% if radio.choice_value == 'value1' %}
radio.tag is a method that returns the entire HTML of the input, which is why your comparison fails.

GAE template code to check is item in the list

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

Categories

Resources