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
Related
Im having some issues with my html content not showing up after putting in some {% if %} {% endif %} statements in my templates
I have this snippet in my code where I display the checkout table if and only if the current user's username matches the one from my Order model. (order.customer_name has a foreign key that is set to the current users username)
{% for order in latest_order %}
{% if user.username == order.customer_name %}
<tr>
<td>{{ order.order_name }}</td>
<td>
<form method="post">
{% csrf_token %}
<button type="submit" name="remove_quantity" value="{{ order.id }}" class="mr-3 btn
btn-outline-info">-</button>
{{ order.order_quantity }}
<button type="submit" name="add_quantity" value="{{ order.id }}" class="ml-3 btn btn-
outline-info">+</button>
</form>
</td>
<td>${{ order.order_individual_price }}</td>
<form method="post">
{% csrf_token %}
<th><button type="submit" name="delete" value="{{ order.id }}" class="btn btn-
danger">Delete Item</button></th>
</form>
</tr>
{% endif %}
{% endfor %}`
I tried the following to see if it prints out the same username, which it does
<h1>{{ user.username }}</h1>
{% for order in latest_order %}
<h1>{{ order.customer_name }}</h1>
{% endfor %}
Picture of the page with user.username and order.customername as h1
When I delete the if statement, this is what the website SHOULD look like
Proper working table
Im pretty sure I'm missing something very obvious, Any help is appreciated!
Here is the code I'm trying to run in my Flask App
{% extends "bootstrap/base.html" %}
{% block title %}Testing title{% endblock %}
{% block content %}
<div class="container">
<h1>Posts</h1>
<h3>Postings</h3>
<form action="/" method="post">
<input hidden placeholder="Name" name="name">
<input placeholder="Post whatever you want..." name="post" required>
<button class="btn btn-primary" type="submit" value="Submit">Submit</button>
</form>
{% for post in posts reversed %}
<div>
{{ 'Anonymous' + ': ' + post[2] }}
</div>
{% endfor %}
</div>
{% endblock %}
This is the error I get jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got 'reversed'
Figure reversed would work since I found some examples online that does that
The correct syntax in Jinja2 to loop through a list in reverse order is:
{% for post in posts|reverse %}
{{ post }}
{% endfor %}
I have a form in django:
country=forms.MultipleChoiceField(choices=lista_tari, widget=forms.CheckboxSelectMultiple(),required=True)
What i want is to display this form on multiple columns in the webpage. There are 30 choices, i want them on 6 or 5 or whatever columns.
This is the search.html:
{% block content%}
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock %}
I am brand-new to css, html and even django so any help would be of great help.
Thank you!
{% for field in form %}
{% ifequal forloop.counter 6 %}</ul><ul>{% endifequal %}
<li>{{ field }}</li>
{% endfor %}
you can try something like this
<form method="POST" class="post-form">{% csrf_token %}
<div class="form-check form-check-inline">
{% for field in form %}
{% if forloop.counter|divisibleby:3 %}</div><div class="form-check form-check-inline">{% endif %}
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% endfor %}
</div>
<button type="submit" class="save btn btn-default">Save</button>
</form>
But probably you should use django-bootstrap3 which helps you to integrate django and bootstrap.
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", "")
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.