how to pass the selected check box id of a input tag to views in django and bulk update the selected rows in table - django

** 1.how to pass the selected check box id of a input tag(which is in for loop, so the name and value of input tag are dynamic ) to views in django and bulk update the selected rows of table in django**
<div class="jumbotron">
<form method="post" action="">
{% csrf_token %}
{{form|crispy}}
<input class = "btn btn-primary" type="submit" value="Search">
</form>
<h3 > Model values </h3>
<ul>
<table class="table" id="tab1">
<thead>
{% if teams %}
<tr>
<th>Select</th>
<th>#</th>
<th><b>Logo</b> </th>
<th><b> Team </b></th>
</tr>
</thead>
<tbody>
{% for value in models %}
<tr>
<td><input name="lol" type = "checkbox" value = "{{value.id}}"/> </td>
<td> {{forloop.counter}}</td>
<td> <img class="w3-display-topmiddle w3-container" src="{{ value.logUri.url }}" alt="alt txt" height="910" width="910"></td>
<td> {{ value.name }} </td>
</tr>
{% endfor %}
</tbody>
</table>

**realized that since i am not keeping input tag in form tag the input tag's name values is not captured by request.POST(now changes are dome in html as below ),later in django views i removed the unnecessary keysvalues using dict.pop() and created a list containing on primary keys
in django orm (model.filter(id__in= lst_of_id_captured).update(field='somevalue') **
<form method="post" action="">
{% csrf_token %}
{{form|crispy}}
<h3 > Model values </h3>
<ul>
<table class="table" id="tab1">
<thead>
{% if teams %}
<tr>
<th>Select</th>
<th>#</th>
<th><b>Logo</b> </th>
<th><b> Team </b></th>
</tr>
</thead>
<tbody>
{% for value in models %}
<tr>
<td><input name="lol" type = "checkbox" value = "{{value.id}}"/> </td>
<td> {{forloop.counter}}</td>
<td> <img class="w3-display-topmiddle w3-container" src="{{ value.logUri.url }}" alt="alt txt" height="910" width="910"></td>
<td> {{ value.name }} </td>
</tr>
{% endfor %}
</tbody>
</table>
<input class = "btn btn-primary" type="submit" value="Search">
</form>

Related

problem of django form in posting request

I'm working on a project and I made a form but when I submit sth it doesn't send it to database although in cmd it shows that request method is post.I really don'tkhow how to deal with it.
thats template code:
{%extends 'base.html'%}
{%block title%}
<title>projects</title>
{%endblock title%}
{%block content%}
<div class="container">
</br>
<form method="post">
{% csrf_token %}
<div class="form-group">
<input type="text" class="form-control" name="project" placeholder="new project?">
</div>
<button type="submit" class="btn btn-primary">add</button>
</form>
</br>
</br>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col"> project-title</th>
<th scope="col">update</th>
<th scope="col">delete</th>
<th scope="col">divide</th>
</tr>
</thead>
<tbody>
{% for obj in all_projects %}
<tr>
<td>{{ obj.proTitle }}</td>
<td>{{ obj.done }}</td>
<td>Delete</td>
<td>Devide</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{%endblock content%}
any advise will be greatly appreciated:))
I solved it ;
in the input tag i put name="project" but the attribute that I defined in my model was "proTitle"
so when I chnged it the problem solved.

how to update django databsae from html?

I want to only update the brand. So I want to see if I can get the post from the input.
print(request.POST.get('brand')) didn't print anything, but I can get print(request.POST.get('url')). Does anyone know why?
Here is my base.html code.
<form action="{% url 'script' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group w-75">
<input type="text" name="url" id="url" class="form-control">
</br>
<button type="submit" class="btn btn-success btn-lg btn-block">submit</button>
</div>
</br>
{% if alldata %}
<table class="table table-striped table-sm" id="">
<thead>
<tr>
<th>Price</th>
<th>Asin</th>
<th>Rank</th>
<th>Brand</th>
<th>CPU</th>
<th>Update</th>
</tr>
</thead>
<tbody>
{% for data in alldata %}
<tr>
<td>{{ data.price }}</td>
<td> {{ data.asin }} </td>
<td>{{ data.rank }}</td>
<td>
<div>
{{ data.brand }} <input type="text" name="brand" id="brand" value="{{data.brand}}">
<button type="submit">Update</button>
</div>
</td>
<td>cpu</td>
<td><button type="submit">Update</button></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</form>
To check All data in request.POST:
print(request.POST)
to print only one 'field'
print(request.POST['field'])

NoReverseMatch at /kitty_view Reverse for 'kitty' with arguments '(5,)' not found. Same url i have tried for another app and that is working [duplicate]

This question already has an answer here:
NoReverseMatch at /kitty_view Reverse for 'kitty' with arguments '(5,)' not found. 1 pattern(s) tried: ['kitty$']
(1 answer)
Closed 3 years ago.
// While rendering this kitty_view I am getting this error. Exactly the same thing I have copied from another app that is working properly. Kindly help. Exactly same thing is working for some other app.
view.py
------------------
def kitty_view(request):
kitty_list = kitty.objects.all().order_by('-cretime')
code1 = str(request.GET.get('Code'))
name1 = str(request.GET.get('nam'))
status1 = str(request.GET.get('stat'))
if (name1 is not None and name1 != ''):
kitty_list = kitty_list.filter(name=name1)
if (code1 is not None and code1 != ''):
kitty_list = kitty_list.filter(code='K00001')
if (status1 is not None and status1 != ''):
kitty_list = kitty_list.filter(status = 'A')
ctx = {'kitty': kitty_list}
return render(request, 'kitty/kitty_view.html', ctx)
Url.py
-----
urlpatterns = [
path('',views.index,name='index'),
path('kitty_view',views.kitty_view,name='kitty_view')
]
template
---------
<form class="form-signin" action="{% url 'kitty_view' %}" method="get">
{% csrf_token %}
<div class="form-row">
<div class="mb-3">
<select class="custom-select center-block" name="code" id="code">
<option value="">Choose Kitty...</option>
<!-- <option>{{ kitty1.code }}</option>
{% for i in kitty1 %}
<option value="{{ i.code }}"> {{ i.code|add:' - '|add:i.name }} </option>
{% endfor %} -->
<option>K00004</option>
<option>K00005</option>
</select>
</div>
<div class="mb-3">
<input type="text" name="nam" id="nam" class="form-control-sm center-block" placeholder="Name" autofocus>
</div>
<div class="mb-3">
<select class="custom-select center-block" name="stat" id="stat" placeholder="Status">
<option value="">Choose Status...</option>
<option>A</option>
<option>I</option>
</select>
</div>
<div class="mb-3">
<!-- Search -->
<button type="submit" class=" btn btn-info " role="button">Search</button>
</div>
</div>
</form>
<table class="table table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Kitty Code</th>
<th scope="col">Name</th>
<th scope="col">Type</th>
<th scope="col">Start Date</th>
<th scope="col">Total Months</th>
<th scope="col">End Date</th>
<th scope="col">Total Members</th>
<th scope="col">Amount</th>
<th scope="col">Installment</th>
<th scope="col">Status</th>
<th scope="col">Details</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
{% if kitty %}
<h1>Biswajit1</h1>
{% for i in kitty %}
<h1>Biswajit</h1>
<tbody>
<tr>
<td>{{ i.id }} </td>
<td>{{ i.code }} </td>
<td>{{ i.name }} </td>
<td>{{ i.type }} </td>
<td>{{ i.startdate }} </td>
<td>{{ i.noofmonths }} </td>
<td>{{ i.enddate }} </td>
<td>{{ i.totalmembers }} </td>
<td>{{ i.totalamount }} </td>
<td>{{ i.noofinstallments }} </td>
<td>{% if i.status == 'A' %}
{{ 'Active' }}
{% else %}
{{ 'Inactive' }}
{% endif %}
</td>
</tr>
</tbody>
{% endfor %}
{% endif %}
</table>
{% endblock %}

Toggle Checkbox in Django and Write to Database

I have some model
from django.db import models
class Model1(models.Model):
title = models.CharField(max_length=300)
boolean1 = models.BooleanField(default=False)
boolean2 = models.BooleanField(default=False)
and am currently displaying all database entries in a template via the following bootstrap table:
<div class = 'table-responsive'>
<table class='table table-striped'>
<thead>
<th>Title</th>
<th>Boolean 1</th>
<th>Boolean 2</th>
</thead>
{% if model1_list %}
{% for Model1 in model1_list %}
<tbody>
<tr>
<td>{{ Model1.title }}</td>
<td>{{ Model1.boolean1 }}</td>
<td>{{ Model1.boolean2 }}</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
I'd like to display the boolean values as checkboxes rather than text, and to be able to check and uncheck the values and have it be reflected in the database (i.e., if the database is checked, the value should be True).
What would be a way to implement this?
Just use IF's:
<div class = 'table-responsive'>
<table class='table table-striped'>
<thead>
<th>Title</th>
<th>Boolean 1</th>
<th>Boolean 2</th>
</thead>
{% if model1_list %}
{% for Model1 in model1_list %}
<tbody>
<tr>
<td>{{ Model1.title }}</td>
<td>
<div class="checkbox">
<label><input type="checkbox" {% if Model1.boolean1 %} checked {% endif %}>Boolean 1</label>
</div>
</td>
<td>
<div class="checkbox">
<label><input type="checkbox" {% if Model1.boolean2 %} checked {% endif %}>Boolean 2</label>
</div>
</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
When the user clicks, you want to send this information to the DB without refreshing the page? If yes, you need to do an ajax request to set this information to DB using JSON.Something like that:
$.ajaxFunction = function(){
var boolean1 = $("#boolean1").val(),
boolean2 = $("#boolean2").val(),
csrf_token = $("#csrf_token").val();
data_send = {
"boolean1": boolean1,
"boolean2": boolean2,
"csrfmiddlewaretoken": csrf_token
};
//jquery
$.ajax({
type: "post",
url: $("/url/ajax/").val(),
data: data_send,
success: function(data_receive){
if(data['boolean1']==true){
...
}
...
}
});
}
If no, you can just do a to POST this information and refresh the page!

Displaying Unescaped in Django Template

I have a form with a title and body but for certain cases the title should be autofilled to a value from the page:
To solve this I used a hidden input type:
<form action="" method="POST"> {% csrf_token %}
<table>
<tr>
<td>
<b>Title: </b>
</td>
{% url 'forum:new_topic' forum.pk as the_url %}
{% ifequal request.path the_url %}
<td>
{{ modelform.title}}
</td>
{% else %}
<td>
{% autoescape off %}
<input type="hidden" value="{{ modelform.title}}" >
{% endautoescape %}
Re:{{ thread.title }}
</td>
{% endifequal %}
</tr>
<tr>
<td>
<b>Body: </b>
</td>
<td>
{{ modelform.body}}
</td>
</tr>
</table>
<input type="submit" value="Submit" />
</form>
However the way it displays on the page is "> Re: .... and is for some reason not escaping the ending quote and >. I tried single quotes but that prevents submission.
Not sure what direction I should go in.