Django formset missing id for manually rendered template - python

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?

Related

How to get class attributes in HTML

I have a few forms in my forms variable, which I took from my DB.
views.py:
def settings(request):
new_form = TrafficSourcesForm()
forms = [TrafficSourcesForm(instance=x) for x in TrafficSources.objects.all()]
return render(request, 'mainpage/dashboard.html', {'new_form': new_form, 'forms': forms, 'error': error})
MY HTML:
<h3>{{ error }}</h3>
{% for form in forms %}
<form method="POST" id="{{form.name.name}}">{% csrf_token %}</form>
{% endfor %}
<form method="POST" id="new-form"> {% csrf_token %}</form>
{% for form in forms %}
<tr>
<td>{{ form.name }}</td>
<td>{{ form.token }}</td>
<td><button class="btn btn-lg btn-success w-100">Save</button></td>
</tr>
{% endfor %}
<tr>
<td><input class="form-control" placeholder="Name" form="new-form"></td>
<td><input class="form-control" placeholder="API-token" form="new-form"></td>
<td><button class="btn btn-lg btn-success w-100" form="new-form">Add</button></td>
</tr>
I am making a kind of editable grid and using a table for my layout ( so I cannot put a form direct to a row). So I am making the forms separately with the new HTML 5 form tag.
But I cannot take out the name(HTML attr on my inputs) which == the name field in the DB. So I could make different forms for every single row in my database. Can you help me?
I was thinking about setting the id of the form from my forms object but it makes the same forms for every row.

Django formset error for ID "Select a valid choice. That choice is not one of the available choices"

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.

how to get multiple data when update data with django?

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)

Reading data of multiple level of multiple checkboxes in Django

I have a page that is something like below:
<table>
<tr>
<th>ID</th>
<th>Edit</th>
</tr>
<form action="/edit_submit/" method="POST">
{% for item in result %}
<tr>
<td>{{ item.id }}</td>
<td>
<input type="checkbox" name="options" id={{ item.id }} value="1">Apple
<input type="checkbox" name="options" id={{ item.id }} value="2">Water
<input type="checkbox" name="options" id={{ item.id }} value="3">Cake
{% csrf_token %}
</td>
</tr>
{% endfor %}
</table>
<input type="submit" value="Submit">
</form>
I need a way to read all the checkboxes value with name options on the page during submit, without knowing the item.id values. Hence I need an output which gives me tuples as item.id,value,checked.
I know that id is not unique here, however is there a better solution for grouping checkboxes? I can put item.id as name in html, but remember I do not know the item.id which appeared on the page.

How do I render form ChoiceField images next to choices without breaking the form?

I am trying to render the images of the choices next to their respective choice. Attempting to do so will not save the form as valid so I have become lost at what to do. I've tried both methods below and I have no idea why one works and the other doesn't, could I get some tips?
#Form:
class ServerGroupForm(forms.Form):
OPTIONS = (
("pic1", "https://i.imgur.com/tMahp6U.png"),
("pic2", "https://i.imgur.com/b76nwsj.gif"),
("pic3", "https://i.imgur.com/qzEcfyX.png Lover"),
("pic4", "https://i.imgur.com/kdc7UF7.png"),
("pic5", "https://i.imgur.com/ynWJ13W.gif"),
("pic6!", "https://i.imgur.com/goHFWsp.png"),
("pic7", "https://i.imgur.com/b76nwsj.gif"),
("pic8", "https://i.imgur.com/KPgKm79.png"),
("pic9", "https://i.imgur.com/7KtEV1i.png"),
("pic10", "https://i.imgur.com/7KtEV1i.png"),
("pic11", "https://i.imgur.com/FXfo773.png")
)
servergroups = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=OPTIONS)
#View:
def sendmessage(msg):
#Other code sends msg to user, not includes so this isn't long
def select_server_group(request):
form = ServerGroupForm(request.POST)
if form.is_valid():
servergroups = form.cleaned_data['servergroups']
sendmessage(msg=servergroups)
return redirect('/')
return render_to_response('webts3/selcectsgroup.html', {'form':form },
context_instance=RequestContext(request))
#HTML: Works but no icons
<section class="login">
<div class="titulo">Create a channel</div>
<form method="post" action="." enctype="multipart/form-data">{% csrf_token %}
<table border="0">
{{ form.as_table }}
</table>
<input type="submit" class="btn btn-block btn-danger" value="Submit" style="margin-top: 10px;">
</form>
</section>
#HTML: Icons but not working
<form method='post' action="." enctype="multipart/form-data">{% csrf_token %}>
<table>
{% for x,y in form.fields.servergroups.choices %}
<tr>
<td><input type="checkbox" name="{{ x }}" value="{{ x }}"><img src={{ y }}</img></td>
</tr>
{% endfor %}
</table>
<input type='submit' value='submit'>
</form>
The name attribute of the field should not be {{ x }}. It should be "servergroups".
Note you'd also need to have some logic that determines if the field is already selected, for when for example the form is being redisplayed after validation errors.

Categories

Resources