I am using django formset to submit a dynamically generated form (based on a file uploaded by the user). The template renders this form manually (I prefer this because it's easier for me to work on style+HTML together in template). On submitting the form, I get an error that id is a required field. My form template looks like this:
<form method="post" id="add3">
{{ formset.management_form }}
{% csrf_token %}
<table id="forms">
<tbody>
{% for lst in result %}
<input type="hidden" name="form-{{ forloop.counter0 }}-id" value="{{ forloop.counter }}" id="id_form-{{ forloop.counter0 }}-id">
<tr>
<td>
<input type="hidden" name="form-{{ forloop.counter0 }}-expense" id="id_form-{{ forloop.counter0 }}-expense" value="{{lst.0}}"/>
</td>
<td>
<input type="hidden" name="form-{{ forloop.counter0 }}-amount" id="id_form-{{ forloop.counter0 }}-amount" value="{{lst.1}}"/>
</td>
{% endfor %}
</tbody>
</table>
</form>
This is a short version. I have 6 fields in each row. I am able to get all 6 fields but it complains about the id. I have added the hidden id input type in each row but that doesn't work. How can I fix this?
I am posting a formset in Django. The form on the client side is generated dynamically using a file uploaded by the user. I render it like this:
<form method="post" id="add3">
{{ formset.management_form }}
{% csrf_token %}
<table id="forms">
<tbody>
{% for lst in result %}
<input type="hidden" name="form-{{ forloop.counter0 }}-id" value="{{ forloop.counter }}" id="id_form-{{ forloop.counter0 }}-id">
<tr>
<td>
<input type="hidden" name="form-{{ forloop.counter0 }}-expense" id="id_form-{{ forloop.counter0 }}-expense" value="{{lst.0}}"/>
</td>
<td>
<input type="hidden" name="form-{{ forloop.counter0 }}-amount" id="id_form-{{ forloop.counter0 }}-amount" value="{{lst.1}}"/>
</td>
{% endfor %}
</tbody>
</table>
</form>
I get the following error on the server side when I receive the formset. Formset is_valid returns False and I get the error:
id: Select a valid choice. That choice is not one of the available choices
How can I fix this? What's the right way to pass an ID?
Note: I am updating management_form total-forms using Javascript. If that matters.
I'm working on a Flask App where I create a form with RadioFields as choices, and I also pass a param2 dict through to the jinja client-side.
I successfully render the individual Radiofields as subfields but I can not seem to render the text for the dict value that accompanies the key.
The label of the radiofield is also the key of the dict in param2, and I'm trying to get it's value printed next to the subfield.label text.
param2 looks like this:
param2{'Text Label1': 'completed', 'Text Label2': 'busy'}
When I print {{ param2 }} on the page, I get the dict as it should be.
{'Text Label1': 'completed', 'Text Label2': 'busy'}
Here is how I print the subfield and call the dict.
<form method="post">
<table>
{% for subfield in form.display %}
<tr>
<td> {{ form.csrf_token }} </td>
<td> {{ subfield }} {{ subfield.label }} </td>
<td> {{ param2[subfield.label] }} </td>
</tr>
{% endfor %}
</table>
<input type="submit" value="Generate">
<button type="reset" value="Reset">Reset</button>
</form>
When the code renders, the param2's <td> </td> is empty:
<tr>
<td> <input id="display-0" name="display" type="radio" value="5"> <label for="display-0">Text Label1</label> </td>
<td> </td>
</tr>
Output I'm trying to get
<> Text Label1 completed
<> Text Label2 busy
I found the problem.
As #sergey-shubin said, the subfield.label property of the form is an HTML construct,
meaning:
if you call {% subfield.label %}
then you will get <label for="display-0">Text Label1</label>. <-- HTML Construct
Solution:
You can extract the label itself from the construct by calling the text method subfield.label.text which yields Text Label1 from the HTML construct.
Answer found here
I want to update data using multiple checkbox
this is my view.py
def update_kel_stat(request, id):
if request.method == "POST":
cursor = connection.cursor()
sql = "UPDATE keluargapeg_dipkeluargapeg SET KelStatApprov='3' WHERE (PegUser = %s )" % id
cursor.execute(sql)
and this is my template.html
<form method="post" action="" name="kel" enctype="multipart/form-data">
{% for keluarga in kels %}
<tr id="{{ keluarga.KelID }}">
<td>
{{ keluarga.KelNamaLengkap }}
</td>
<td>{{ keluarga.KelStatApprov }}</td>
<td>{{ keluarga.KelKetRevisi }}</td>
<td><input type="checkbox" name="kel[]"
value="{{ keluarga.KelID }}"></td>
</tr>
{% endfor %}
<tr>
<td>
<button type="button" name="btn_delete" id="btn_delete"
class="btn btn-success"
onClick="setDeleteAction();">Approve
</button>
</td>
</tr>
</form>
how to get multiple value from checkbox in template.html to django view.py?
First of all it is not recommended to user raw SQL for such simple queries. It is quite easy to update some record with django ORM:
Entry.objects.filter(id=10).update(comments_on=False)
As for your question you can do it this way. In your template:
{% for keluarga in kels %}
<input type="checkbox" name="kel" id="kel{{ forloop.counter }}" value="{{ keluarga.KelID }}">
<label for="kel{{ forloop.counter }}">Choice {{ forloop.counter }}</label>
{% endfor %}
And in your view:
kels = request.POST.getlist('kel')
Kel.objects.filter(id__in=kels).update(StatApprov=3)
I have a list of members, from a for loop in jinja2 with select and option
I would like to be able to select one of the members from the for loop,
and add that member to another list of entries to an event, with the extra options available to the user. (getting the member is easy, from the value of the button, getting the options is the problem)
For example:
Jinja2 Template:
<form method="post" action="add_member_to_list">
<table>
{% for member in members %}
<tr>
<th>{{ member.name }}</th>
<td><input type="checkbox" name="in_out"></td>
<td><select name="day">
<option>Monday</option>
<option>Wednesday</option>
</select>
</td>
<td><button type="submit" name="id" value="{{ member.id }}">Add</button></td>
</tr>
{% endfor %}
</table>
</form>
in Google App Engine - Python
class AddUser(BaseHandler):
def post(self):
in_out = self.request.get("in_out")
id = self.request.get("id")
day = self.request.get("day")
In this scenario, self.response.get("day") only ever returns the option selected for the first iteration of the for loop.
The checkbox; self.request.get("in_out"), returns on or off as expected.
So submitting the nth iteration of the for loop:
"Monday" is always returned from select/option.
How can I get the select/option corresponding to the nth iteration of the for loop?
Thanks!
The simplest solution will be to move the form tag into your for loop
<table>
{% for member in members %}
<form method="post" action="add_member_to_list">
<tr>
<th>{{ member.name }}</th>
<td><input type="checkbox" name="in_out"></td>
<td><select name="day">
<option>Monday</option>
<option>Wednesday</option>
</select>
</td>
<td><button type="submit" name="id" value="{{ member.id }}">Add</button></td>
</tr>
</form>
{% endfor %}
</table>
So your resulting form has multiple fields with the same name, right?
Like for the first member, there is a and for the second member, there is also a ?
If so, forms don't support that. You have to have a unique name for each field. You'll have to throw in a memberID on each iteration of the loop, or come up with a better design. Something like <select name="day{{member.id}}"> would be a hacky fix.
Another way is you just leave the in_out and day OUT of the for loop, keep it outside.
I hope that helps.
I found a quite simple solution for this:
instead of
day = self.request.get("day")
I used
day = self.request.get_all("day")
Which returns a list of all the options from the select, including the option the user choose for the member.
Then, for the Add Button, instead of
<td><button type="submit" name="id" value="{{ member.id }}">Add</button></td>
I assigned the name "loop" and a value of {{ loop.index0}}
so,
self.request.get("loop")
returns the position of the member selected, corresponding the position on the day list of the target. voila.
The member I get from a hidden td instead of from the submit button.
The final code:
<form method="post" action="add_member_to_list">
<table>
{% for member in members %}
<tr>
<th>{{ member.name }}</th>
<td><input type="checkbox" name="in_out"></td>
<td>
<select name="day">
<option>Monday</option>
<option>Wednesday</option>
</select>
</td>
<td class="hidden"><input name="id" value="{{ member.id }}"></td>
<td><button type="submit" name="loop" value="{{ loop.index0 }}">Add</button></td>
</tr>
{% endfor %}
</table>
</form>
class AddUser(BaseHandler):
def post(self):
day = self.request.get_all("day")
id = self.request.get_all("id")
loop = self.request.get("loop")
target_day = day[int(loop)]
target_id = id[int(loop)]
Niiice.