Learning to use Jinja2 templates, missing URL values - python

I'm learning how to use Jinja2 templates (see code below). When I add an item to my form, I expect that the url will change to something like this.
http://localhost:8080/?food=steak&food=eggs&food=cheese
However, what ends up happening is that the first food will have a value, but everything else will be blank. It looks something like this:
http://localhost:8080/?food=asd&food=&food=
What am I doing wrong?
<form>
<h2>Add a Food</h2>
<input type="text" name="food">
{% if items %}
{% for items in items %}
<input type="hidden" name="food" value="{{item}}">
{% endfor %}
{% endif %}
<button>Add</button>
{% if items %}
<br>
<br>
<h2>Shopping List</h2>
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endif %}
</form>
Here is the function to render the HTML:
class MainPage(Handler):
def get(self):
items = self.request.get_all("food")
self.render("shopping_list.html", items=items)

For item in items?
{% for item in items %}
<input type="hidden" name="food" value="{{item}}">
{% endfor %}

I'm not certain, but I think the issue is with your <button> tag. Try something like <button type='submit'>. I think your current button just doesn't do anything.
w3 documentation here

Related

How to get all the options submitted by form

How to get all the options submitted by the form in Django, this is the form which I used.
{% 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{{question.id}}" value="{{ option.options}}" > {{option.options}}<br>
{% endfor %}
{% endfor %}
<input type="Submit" name="Submit">
</form>
{% endblock%}
I tried selected_choice=request.POST ,but getting this as output csrfmiddlewaretokenchoice1Submitchoice3. How can I solve this? Thank you
In django request.POST is dictionary-like object, see details here.
So to obtain argument choice in view you can use following syntax:
selected_choice=request.POST.get('choice')
which return choice value or None in case this is empty.
Since request.POST is dict-like object you can use items() method to get all values and filter them:
for k, v in request.POST.items():
if k.startswith('choice'):
print(k, v)
this will print only those params with choice text in name.
selected_choice=request.POST.get('choice')
The above should work just fine, but if you're crazy, you can try this:
{% extends 'quiz/base.html' %}
{% block content%}
<h1>You are at quiz page</h1>
<form action="{% url 'quiz:process_data' %}" method="post" id= "crazyform">
{% 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="hidden" name="crazychoice" class="crazy" value="nothing">
<input type="Submit" name="Submit">
</form>
{% endblock%}
Then some JQuery:
$('#crazyform input').on('change', function() {
$(".crazy").val($('input[name=choice]:checked', '#crazyform').val())})
Everytime you click a radio button, the value of the hidden input field changes to the value of the selected radio button.
Then in your view you can:
selected_choice = request.POST.get("crazychoice", "")

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

Don't do something in Base on a Particular template page - Django

I have setup my nav bar in my base.html as follows
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
{% if user.is_authenticated %}
<li>Logout</li>
<li>My Profile</li>
{% else %}
<li>Login</li>
<li>Register</li>
{% endif %}
</ul>
Now the problem is that I Don't want to execute the {% if %} block when user is not authenticated and when I am rendering a particular template page i.e
create_thing.html
{% extends 'layouts/base.html' %}
{% block title %} Create a Thing
- {{ block.super }}{% endblock %}
{% block content %}
<div id="content">
<h1> Create a Thing </h1>
<form role="form" action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
</div>
{% endblock %}
Simply copying the contents from Base and pasting it after removing doesn't help. It still executes base.html and gets inside the if statement and shows an error because slug wont be defined until I have filled details in create_thing.html.
Try nesting the {% if %} clause in another {% if not aux_var %} clause. Whenever you don't want to execute the first if, pass "aux_var":1 to the renderer as context.

two-dimensional array Django

I have a two-dimension array in my django app which I need to pass to html template.
How I can make HTML handle two-dimensional array?
{% block content %}
<h2>Survey</h2>
<form>
{% for q in question %}
<p>{{q[0]}}</p>
<p>{{q[1]}}</p>
{% endfor %}
<input type="submit" value="submit">
</form>
{% endblock %}
I got error:
Could not parse the remainder: '[0]' from 'q[0]'
{% block content %}
<h2>Survey</h2>
<form>
{% for q in question %}
<p>{{q.0}}</p>
<p>{{q.1}}</p>
{% endfor %}
<input type="submit" value="submit">
</form>
{% endblock %}
Django just doesn't have a pre-setup way for passing multi-dimensional arrays to templates.
The best work-around is to separate the columns out manually like so... ("i" corresponds to each row in final output)
data = {}
for i, question_answer in enumerate(question_answer_pairs):
data[i]= {
'question':question_answer.question,
'answer':question_answer.answer
}
return render(request, 'survey.html', data)
Then in your template
{% block content %}
<h2>Survey</h2>
<form>
{% for key,value in data.items %}
<p>{{value.question}}</p>
<p>{{value.answer}}</p>
{% endfor %}
<input type="submit" value="submit">
</form>
{% endblock %}

Problem with logic in Django template

Supose this portion of a Django template. regs is a list of Reg objects. Reg.editable is a BooleanField.
I want to render a radio button per element in the list. If r.editable is False, the radio button must be disabled:
{% for r in regs %}
<input type="radio" value="{{ forloop.counter }}"
{% if forloop.first %}checked="checked"{% endif %}
{% if not r.editable %}disabled="disabled"{% endif %}/>
{% endfor %}
As you can see, I'm using forloop.first to check the first radio button, but this has a problem! What about if the first element has editable == False? The first radio button will be rendered disabled and chequed. If then a user submit "the form" I'll receive a value not expected.
Am I clear with the problem here? How can I rewrite this template to render as checked the FIRST ENABLED radio button?
Thanks
Djangos templating language doesn't give you a lot in the way of logic in the template (I've heard positive things about Jinja2 if you want to change that). There's also the "Smart" {% if %} tag which adds some more functionality and incidentally is being proposed for inclusion in Django 1.2.
As for solving this problem I would most likely move the logic over to the view. (Disclaimer: don't have the time to test this code snippet but it should provide the general idea)
def my_view(request, *args, **kwargs):
# QuerySet is fetched however it's done...
regs = Regs.objects.all()
# Wrap regs in display information
checked = False
radio_buttons = []
for r in regs:
if r.editable:
if not checked:
radio_buttons.append( { 'checked':True, 'enabled':True, 'object':r } )
# Set to true once
checked = True
else:
radio_buttons.append( { 'checked':False, 'enabled':True, 'object':r } )
else:
radio_buttons.append( { 'checked':False, 'enabled':False, 'object':r } )
# Then pass in radio_buttons for the value of regs down here
render_to_whatever(..., { 'regs':radio_buttons } )
In this case we've wrapped the QuerySet which will give our template some more details about rendering. The template becomes "dumb" now.
{% for r in regs %}
<input type="radio" value="{{ forloop.counter }}"
{% if r.checked %}checked="checked"{% endif %}
{% if not r.enabled %}disabled="disabled"{% endif %}/>
{% comment %} To access the original object use: {{ r.object }} {% endcomment %}
{% endfor %}
The real answer to this question is as follows:
Such logic has no place in the template. You can preprocess the context before passing it to the template, thus eleminating the need to do this using the (intentionally) crippled template engine logic.
In my opinion, what you are doing is wrong. I mean, django has perfectly fine forms api, why render the inputs directly then? Some might argue, that django's forms api is inflexible, but for this specific need it will undoubtely be sufficient.
And to reiterate - this kind of logic does not belong in the presentation layer. So don't put it there, it will bite you. In fact it already did.
Just adjust your ifs
{% for r in regs %}
{% if forloop.first %}
<input type="radio" value="{{ forloop.counter }}" checked="checked"/>
{% else %}
{% if not r.editable %}
<input type="radio" value="{{ forloop.counter }}" disabled="disabled"/>
{% endif %}
{% endif %}
{% endfor %}
PS: Your question did not clearly explain what you wanted. I made some reasonable assumption. Update the question if what you want is something else.
Similar to T. Stone's answer of doing this logic in the view, you could just add a new template variable that indicated the first checked radio:
def my_view(request, *args, **kwargs):
regs = Regs.objects.all()
checked_index = None
for i, reg in enumerate(regs):
if reg.enabled:
checked_index = i
break
# pass checked_index into the template...
Template:
{% for r in regs %}
{% ifequal forloop.counter0 checked_index %}
<input type="radio" value="{{ forloop.counter }}" checked="checked"/>
{% else %}
<input type="radio" value="{{ forloop.counter }}" {% if not r.editable %}disabled="disabled"{% endif %}/>
{% endif %}
{% endfor %}
Similar to becomingGuru but solving your problems:
{% for r in regs %}
{% if not r.editable %}
<input type="radio" value="{{ forloop.counter }}" disabled="disabled"/>
{% else %}
{% if forloop.first %}
<input type="radio" value="{{ forloop.counter }}" checked="checked"/>
{% endif %}
{% endif %}
{% endfor %}
It first checks if r is editable, and then checks if it is the first.
Regards.

Categories

Resources