how to update django databsae from html? - django

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

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

Page is opening but search is not working when i am using {% for i in kitty_list %} but in case of {% for i in kitty %} then NoReverseMatch error

In the below scenario, the page is opening but search functionality is not working when i am using {% for i in kitty_list %} in the template. However, when i am using {% for i in kitty %} then I get a NoReverseMatch error.
Url: path('kitty_view',views.kitty_view,name='kitty_view')
View: kitty_list = kitty_list.filter(status = status1)
kittys = kitty.objects.all()
ctx = {'kitty': kitty_list,'kitty_code':kittys}
return render(request, 'kitty/kitty_view.html', ctx)
This is the HTML page which is giving NoReverseMatch error when {% for i in kitty %} is used. When i am using {% for i in kitty_list %} then the page is rendering but the search button is not working.
template:
{% extends 'base.html' %}
{% load static %}
{% block content %}
<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="code1" id="code1">
<option value="">Choose Kitty...</option>
{% for j in kitty_code %}
<option value="{{ j.code }}"> {{ j.code|add:' - '|add:j.name }} </option>
{% endfor %}
</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">
<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>
</tr>
</thead>
{% if kitty %}
{% for i in kitty_code %}
<tbody>
<tr>
<td>{{ i.id }} </td>
<td>{{ i.code }} </td>
<td>{{ i.name }} </td>
</tr>
</tbody>
{% endfor %}
{% endif %}
</table>
{% endblock %}
Inside the template file, you need to modify the below line. {% url 'kitty_view' %} to {% url '<app_name>:kitty_view' %}. If this would not work then, please give me an error in more detail.

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 %}

How to create such a table with checkbox in django?

I want to have a table with a checkbox on each row with Django as shown in the image. My Django view.py, models.py and HTML file are mentioned below. How this can be done? Is there any built-in function in Django or what?
Table with check box
I have models file as:
class AppsDescription(db.Model):
aws_response = aws_list_conf_api_call()
CHOICES = [("yes", "YES"),
("no", "NO")]
OPTIONS = list()
for apps in aws_response:
OPTIONS.append(('{name1}'.format(name1=apps.lower()), '{name2}'.format(name2=apps)), )
name = db.CharField(max_length=256)
description = db.TextField()
plan_to_migrate = db.CharField(choices=CHOICES, max_length=256)
# app_names = MultiSelectField(choices=OPTIONS)
def __str__(self):
return self.name
My views.py as
def createapp(request):
# import ipdb; ipdb.set_trace()
form = DashboardForm()
if request.method == "POST":
form = DashboardForm(request.POST)
list_of_inputs = request.POST.getlist("inputs")
if form.is_valid:
form.save(commit=True)
return HttpResponseRedirect(reverse("aws:createapp"))
server = aws_server_list_conf()
return render(request, "createapp.html", {'server':server, 'form': form})
My html file as
<form method="POST">
{{form.as_p}}
<table>
<tr>
<th>Select</th>
<th>Agent ID</th>
<th>Configuration ID</th>
<th>Host Name</th>
<th>OS Name</th>
<th>OS Version</th>
<th>Source</th>
<th>Time of Creation</th>
<th>Type</th>
</tr>
{% for apps in server %}
<tr>
<td><input type="checkbox" name="" value=""></td>
{% for k,v in apps.items %}
<td>{{ v }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% csrf_token %}
<input type="submit" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" name="" value="submit">
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="alert alert-success alert-dismissible">
<a class="close" data-dismiss="modal" aria-label="close">×</a>
<strong>Success!</strong> App information stored Successfully.
</div>
</div>
</div>
</form>
I want to have a table with checkbox on each row with django as shown in the image.
Your HTML file needs to look like this:
<form method="POST">
{{form.as_p}}
<table>
<tr>
<th>Select</th>
<th>Agent ID</th>
<th>Configuration ID</th>
<th>Host Name</th>
<th>OS Name</th>
<th>OS Version</th>
<th>Source</th>
<th>Time of Creation</th>
<th>Type</th>
</tr>
{% for apps in server %}
<tr>
{% for k,v in apps.items %}
<td><input type="checkbox" name="selected_options" value="v.id"></td>
<td>{{ v }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% csrf_token %}
<input type="submit" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" name="" value="submit">
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="alert alert-success alert-dismissible">
<a class="close" data-dismiss="modal" aria-label="close">×</a>
<strong>Success!</strong> App information stored Successfully.
</div>
</div>
</div>
</form>