how to get multiple data when update data with django? - python

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)

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 missing id for manually rendered template

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?

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.

Html content not showing up after if statement in Django

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!

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.

Categories

Resources