Django form in a table can't get right parameters - django

<tbody>
{% for sec in sec_list %}
<tr>
<td>{{sec.c_id_id}}.{{sec.sec_id}}</td>
<td>{{sec.title}}</td>
<td>{{sec.name}}</td>
<td>{{sec.time}}</td>
<td>{{sec.r_no_id}}</td>
<td>{{sec.cur}}/{{sec.capcity}}</td>
<td><form method="post" role="form" action="">
{% csrf_token %}
<input class="hidden" type="submit" value="{{sec.c_id_id}}.{{sec.sec_id}}" name="course" id="course">
<input class="hidden" type="submit" value="{{sec.cur}}" name="num" id="num">
<input class="hidden" type="submit" value="{{sec.capcity}}" name="limit" id="limit">
<p class="form-action">
<input type="submit" value="选课" class="btn btn-link">
</p>
</form></td>
</tr>
{% endfor %}
</tbody>
I want to use a hidden form to transfer some parameters in a table.My code is above.But when I use
request.POST.get("limit",'')
,I got a null one.How to fix it

edit your html code by replacing the type="submit" to type="hidden",
<td><form method="post" role="form" action="">
{% csrf_token %}
<input class="hidden" type="hidden" value="{{sec.c_id_id}}.{{sec.sec_id}}" name="course" id="course">
<input class="hidden" type="hidden" value="{{sec.cur}}" name="num" id="num">
<input class="hidden" type="hidden" value="{{sec.capcity}}" name="limit" id="limit">
<p class="form-action">
<input type="submit" value="选课" class="btn btn-link">
</p>
</form></td>

Shouldn't those inputs be type=hidden instead of class=hidden?
<input type="hidden" value="{{sec.capcity}}" name="limit" id="limit">
The type=submit in all of them makes no sense to me as it is supposed to be used only in submit buttons.

Related

TR, TH, TD was created but TABLE NOT

i Need understand it but not be able to.
My ClassForm (in view):
class FormContact(forms.Form):
name = forms.CharField(label='Nome', max_length=100, required=False)
subject = forms.CharField(label='Assunto', max_length=100, required=False)
message = forms.CharField(label='Mensagem', widget=forms.Textarea, required=False)
My extended Template:
{% extends 'site.html' %}
{% load static %}
{% block content %}
<form class="contato" id="contato" action="{% url 'contato' %}" method="post">
<h1>Contato</h1>
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
<script src="{% static 'js/form_contact.js' %}"></script>
{% endblock %}
Now,
in the navigator (inspector -> elements)
<form class="contato" id="contato" action="/contato/" method="post">
<h1>Contato</h1>
<input type="hidden" name="csrfmiddlewaretoken" value="???">
<label for="id_name">Nome:</label>
<input type="text" name="name" maxlength="100" id="id_name">
<label for="id_subject">Assunto:</label>
<input type="text" name="subject" maxlength="100" id="id_subject">
<label for="id_message">Mensagem:</label>
<textarea name="message" cols="40" rows="10" id="id_message"></textarea>
<input type="submit" value="Submit">
/form>
AND, in the navigator (Ctrl+U) View Code
<form class="contato" id="contato" action="/contato/" method="post">
<h1>Contato</h1>
<input type="hidden" name="csrfmiddlewaretoken" value="????">
<tr>
<th><label for="id_name">Nome:</label></th>
<td>
<input type="text" name="name" maxlength="100" id="id_name">
</td>
</tr>
<tr>
<th><label for="id_subject">Assunto:</label></th>
<td>
<input type="text" name="subject" maxlength="100" required id="id_subject">
</td>
</tr>
<tr>
<th><label for="id_message">Mensagem:</label></th>
<td>
<textarea name="message" cols="40" rows="10" required id="id_message"></textarea>
</td>
</tr>
<input type="submit" value="Submit">
</form>
My doubt is:
Why, in the Ctrl+U, was created the elements TR, TH and TD but TABLE not?
I can't understand it
It is this form same?

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: How can I render my formsets without having every single field in a <p> (or any other) element without losing the management form?

I'm rendering my model formset in the following way:
<form method="POST" class="note-form">
{{ formset.management_data }}
{% csrf_token %}
{{ formset.as_p}}
<input type="submit" value="Save">
</form>
However, this renders every single field in every form in a <p> element, which I don't want. It's unpacked like this:
<form method="POST" class="note-form">
<input type="hidden" name="csrfmiddlewaretoken"...>
<input type="hidden" name="form-TOTAL-FORMS"...>
<input type="hidden" name="form-INITIAL-FORMS"...>
<input type="hidden" name="form-MIN-NUM-FORMS"...>
<input type="hidden" name="form-MAX-NUM-FORMS"...>
<p>
<textarea ...></textarea>
</p>
<p>
<input ...>
</p>
<p>
<input ...>
</p>
<p>
<textarea ...></textarea>
</p>
...and so on...
</form>
Instead, I want something that would be rendered like this
<form method="POST" class="note-form">
<input type="hidden" name="csrfmiddlewaretoken"...>
<input type="hidden" name="form-TOTAL-FORMS"...>
<input type="hidden" name="form-INITIAL-FORMS"...>
<input type="hidden" name="form-MIN-NUM-FORMS"...>
<input type="hidden" name="form-MAX-NUM-FORMS"...>
<div ...>
<textarea ...></textarea>
<input ...>
<input ...>
</div>
<div ...>
<textarea ...></textarea>
<input ...>
<input ...>
</div>
....and so on....
That is, each single form in a div element. Now, I tried doing the following
<form method="POST" class="note-form">
{{ formset.management_data }}
{% csrf_token %}
{% for form in formset %}
<div>
{{ form }}
</div>
{% endfor %}
<input type="submit" value="Save">
</form>
This renders the formset the way I want it. The problem with this is that, when I do it, the management data disappears and I get the error 'ManagementForm data is missing or has been tampered with'.
I found something a bit similar in the documentation, but still, this is not working for me. What am I doing wrong?
Try:
<table>
{{ formset.as_table }}
</table>
Apparently, if the formset is not rendered with {{ formset.as_p }}, {{ formset.as_table }} or similar, the management data is not rendered automatically. It has to be rendered manually.
{{ formset.management_data }}
{% csrf_token %}
<div class="notes-pane">
{{ formset.management_form }}
{% for form in formset %}
<div>
{{ form }}
</div>
{% endfor %}
<input type="submit" value="Save">
</div>
</form>

Django: insert multiple input text and file

I have recently tried to learn Django and I want to know it's possible to insert multiple input with this form structure to table database? if it's possible, can you tell me how it is? Thank you!!!
<form class="log-in" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div>
<input type="text" name="title">
<input type="file" name="image">
</div>
<div>
<input type="text" name="title">
<input type="file" name="image">
</div>
<div>
<input type="text" name="title">
<input type="file" name="image">
</div>
<button type="submit" class="btn btn-info">Submit</button>
</form>
What you are looking for are formsets: docs.djangoproject.com/en/3.0/topics/forms/formsets

django allauth: redirecting for invalid passwords

I have a custom page which calls allauth signup view (custom_passcheck.html)
<form action="{% url 'account_signup' %}" method="post" class="form">
{% csrf_token %}
<BR>
<BR>
<BR>
<center>
<div class=signuponepage>
<div class=sign-up>
<div class=signup-box>
<div class="form-element email-address">
<label id="email-address-label">
<strong>
Create a password (it must be 6 characters long)
</strong>
<input type="hidden" name="username" value="{{ pemail }}">
<input type="hidden" name="email" value="{{ email }}">
<input type="text" maxlength="19" name="password1" value="" placeholder="Password">
<input type="text" maxlength="19" name="password2" value="" placeholder="Password (again)">
</label>
</div>
<div class="form-element nextstep-button">
{% buttons %}
<button type="submit" class="btn btn-primary btn-sm">
Finish {% bootstrap_icon "chevron-right" %}
</button>
{% endbuttons %}
</div>
</div>
</div>
</div>
</center>
</form>
Now, if a user puts in password less than 6 chars, it will raise an error and redirect the user to allauth signup page (/account/signup)
Question: instead of redirecting to (/account/signup), how can i redirect to the custom page (custom_passcheck.html) while preserving POST data