Django shopping cart update - django

I am trying to send slg (as quanty when update shopping cart):
{% for item in myCart.values %}
<tr>
<td class="product-col">
<img src="/media/{{item.image}}" alt="">
<div class="pc-title">
<h4>{{item.name}}</h4>
<p>${{item.price}}</p>
</div>
</td>
<td class="quy-col">
<div class="quantity">
<div class="pro-qty">
<input type="number" min="1" name="slg[{{item.id}}]" value="{{item.slg}}">
</div>
</div>
</td>
<td class="size-col"><h4>Size M</h4></td>
<td class="total-col"><h4>${% widthratio item.price 1 item.slg %}</h4></td>
<td class="total-col">Delete</td>
</tr>
{% endfor %}
This is my view, i am trying to get data form templates but it's not work
def updateCart(request):
slg = request.POST['slg']
return render(request, 'home/check.html', {'myCart': slg})

I don't see the section about the way you submit the data. You might want to build a form
like below
<form action="/your-name/" method="post">
<label for="your_name">Your name: </label>
<input type="submit" value="OK">
</form>
and post the data so when you check the request.POST that it has some data in it.
With your code, I don't see any data being posted.

Related

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'])

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

** 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>

Flask Radio buttons with choice for every user in table from database

I tried to create a html table from flask jinja2 template. I'm able to get all the data from database and make a table with rows and columns and radio buttons. But I'm unable to get choice for every user or row that is added to the table.
Unable to figure out how to do that. Should I make use of javascript,jquery or ajax to give radio button choice for every user or is there a way to solve this in flask template.
My code is as below:
<form class="form-horizontal" action="/check_approve" method="post" id="admin_approve">
<div class="table-responsive">
<table class="table table-bordered table-striped table-highlight">
<thead>
<tr>
<th>UserName</th>
<th>Privileges</th>
<th>Approve</th>
<th>Reject</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>{{item[0]}}<input type="hidden" name="empid" value={{item[0]}}></td>
<td>
<select name="Privileges" form="admin_approve">
<option value="empty"> </option>
<option value="HR">HR</option>
<option value="Compensation Analyst">Compensation Analyst</option>
<option value="HR Manager">HR Manager</option>
</select>
</td>
<td>
<div class='radio'>
<label><input type='radio' id='choice1' name='approve' value="approve" checked></label>
</div>
</td>
<td>
<div class='radio'>
<label><input type='radio' id='choice2' name='reject' value="reject" checked></label>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="wrapper">
<button type="submit" form ="admin_approve" class="btn btn-primary">Submit</button>
</div>
</form>
Below is the attached screenshot. Appreciate your help.
table

Taking Attendance in Django Python

I am trying to mark attendance. But I am not able to get the code. My submit should send the input to database for each student. Any suggestion for doing this in better way is welcome.
My models.py is as follows
It has Employee, Mentor and Student and below is the model where values can be changed.
class ClassName(models.Model):
class_name = models.CharField(max_length=20)
class_date=models.DateField(blank=True, null=True)
Mentor= models.ForeignKey(Mentor, related_name='class_mentor')
student = models.ManyToManyField(Student, related_name='class_student')
attendance = models.BooleanField(default=True)
def __str__(self):
return str(self.class_name)
Views.py : This is just to display the markattendance page. I am not able to figure out how to get the value and Post the value to database. Right now, I am fetching Students value from the Students table. I think I should be creating Classname Form and and display students. I was getting Internal server error.
def markattendance(request):
students = Student.objects.all()
return render(request, 'home/markattendance.html',
{'students': students})
markattendance.html : On clicking submit, the attendance should be marked.
{% extends 'home/index.html' %}
{% block content %}
<div class="container-fluid">
<div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Class</th>
<th>Attendance</th>
</tr>
</thead>
<tbody>
{% for students in students %}
{% ifequal students.Men_name|stringformat:"s" user.username %}
<tr>
<td>{{ students.Student_name }}</td>
<td>{{ students.Student_Class }}</td>
<td>
<label class="switch">
<input id="toggle-slider_position()" type="checkbox">
<span class="slider round"></span>
</label>
</td>
</tr>
{% endifequal %}
{% endfor %}
</tbody>
</table>
</div>
<br/>
<div class="row" >
<div class="col-sm-4"></div>
<div class='col-sm-4' align="center">
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<button class="btn btn-lg">Submit</button>
</div>
<div class="col-sm-4">
</div>
</div>
<br/><br/><br/><br/><br/>
</div>
</div>
</div>
{% endblock %}
Please help me out. Any suggestion is welcome.

Django: create generic link from radio select

i have a index.html that display all databases i own. And now i wanna give some options to handle the database (e.g. edit, delete, ...). So i wanna create a radio select with the database-ids and some links for redirecting the right url.
My aim: click on the requested action brings the user to the right url.
I hope, that was understandable :)
Can anyone help me, please.
// edited
<script>
function edit_db(id){
window.location.href="/edit/"+id
}
function delete_db(id){
window.location.href="/"+id+"/delete/"
}
</script>
<form method="post" action="">
<ul>
<table>
<tr>
<th> </th>
<th>Database</th>
<th>expireDate</th>
</tr>
{% for db in resources %}
<tr>
<td>
<input type="radio" name="id" id="{{db.id}}" value="{{db.id}}">
</td>
<td>{{db.name}}</td>
<td>{{db.expireDate}}</td>
</tr>
{% endfor %}
</table>
<input type="submit" class="submitButton" onclick="edit_db({{ db.id }})" value="{% trans "Edit database"%}" />
<input type="submit" class="submitButton" onclick="delete_db({{ db.id }})" value="{% trans "Delete database"%}" />
</form>
I'd suggest you use jQuery or so to accomplish this.
However, you need to adjust your id's first, because an id/class can't start with a number in html, so rather use id="database_{{ db.id }}"
I don't know what {% show_task_link "./{{db.id}}/edit" _("Edit database") %} does, but make sure it has an id so you can access it through jQuery which would be something like:
<script type="text/javascript>
$(function(){
$('.db').click(function(){
if($(this).is(':checked')){
$('#id_for_your_link').html() // put your link in html, add the id through $(this).val()
}
}
})
</script>
Note: i haven't tested the jQuery
<script>
function edit_db(id){
window.location.href="/edit/"+id
}
function delete_db(id){
window.location.href="/"+id+"/delete/"
}
</script>
<form method="post" action="">
<ul>
<table>
<tr>
<th> </th>
<th>Database</th>
<th>expireDate</th>
</tr>
{% for db in resources %}
<tr>
<td>
<input type="radio" name="id" id="{{db.id}}" value="{{db.id}}">
</td>
<td>{{db.name}}</td>
<td>{{db.expireDate}}</td>
<td>
<input type="submit" class="submitButton" onclick="edit_db({{ db.id }})" value="{% trans "Edit database"%}" />
<input type="submit" class="submitButton" onclick="delete_db({{ db.id }})" value="{% trans "Delete database"%}" />
</td>
</tr>
{% endfor %}
</table>
</form>