Django Inserting data (Getting correct data) - django

I have this code in my html
<form method="post" id="DogForm" action="/studentbehavior/" class="myform" style="width: 100%" enctype="multipart/form-data">{% csrf_token %}
<table class="tblcore">
<input type="hidden" value="{{teacher}}" name="teacher">
<tr>
<td rowspan="2" colspan="2">Core Values</td>
{% for core in corevalues %}
<td colspan="8"><input type="hidden" value="{{core.id}}" name="core">{{core.Description}}</td>
{% endfor %}
</tr>
<tr>
{% for corevalues in corevaluesperiod %}
<td colspan="4" style="font-size: 12px"><input type="hidden" value="{{corevalues.id}}" name="coredescription">{{corevalues.Description}}</td>
{% endfor %}
</tr>
<tr>
<td colspan="2">Student's Name</td>
{% for corevalues in period %}
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
{% endfor %}
</tr>
{% for students in student %}
<tr>
<td colspan="2" class="names"><input type="hidden" value="{{students.id}}" name="student">{{students.Students_Enrollment_Records.Students_Enrollment_Records.Students_Enrollment_Records.Student_Users}}</td>
<td colspan="4">
<select name="marking">
<option>--------</option>
{% for m in marking %}
<option value="{{m.Marking}}" >{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select name="marking">
<option >--------</option>
{% for m in marking %}
<option value="{{m.Marking}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select name="marking">
<option>--------</option>
{% for m in marking %}
<option value="{{m.Marking}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select name="marking">
<option>--------</option>
{% for m in marking %}
<option value="{{m.Marking}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select name="marking">
<option>--------</option>
{% for m in marking %}
<option value="{{m.Marking}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select name="marking">
<option>--------</option>
{% for m in marking %}
<option value="{{m.Marking}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select name="marking">
<option>--------</option>
{% for m in marking %}
<option value="{{m.Marking}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
</tr>
{% endfor %}
</table>
<input type="submit" value="Insert" id="submit">
</form>
this is what its looks like in web view
this is my code in my views where the data inserted
def studentbehavior(request):
id = request.POST.get('teacher')
teacher = EmployeeUser(id=id)
coreid = request.POST.get('core')
core = CoreValues(id=coreid)
corevalues = []
for corevaluesid in request.POST.getlist('coredescription'):
corevalues.append(corevaluesid)
coreperiodID = request.POST.get('coreperiod')
coreperiod = gradingPeriod(id = coreperiodID)
marking = []
for markingID in request.POST.getlist('marking'):
marking.append(markingID)
print(marking.append(markingID))
for m in request.POST.getlist('marking'):
for student in request.POST.getlist('student'):
students = StudentPeriodSummary(id=student)
#s = marking[m]
for desc in request.POST.getlist('coredescription'):
coredescription = CoreValuesDescription(id=desc)
V_insert_data = StudentsCoreValuesDescription(
Teacher=teacher,
Core_Values=coredescription,
Marking=m,
Students_Enrollment_Records=students,
grading_Period=coreperiod,
)
V_insert_data.save()
return render(request, "Homepage/updatebehavior.html")
this my problem, all markings saved to the datavase even if I select only one in the selection box, (in admin view), looks like the data entering in the database is incorrect.
this is my desire answer
UPDATE
WHEN I try the answer of mr#Saisiva A
for m, student, desc in zip(request.POST.getlist('marking'), request.POST.getlist('student'),
request.POST.getlist('coredescription')):
coredescription = CoreValuesDescription(id=desc)
students = StudentPeriodSummary(id=student)
V_insert_data = StudentsCoreValuesDescription(
Teacher=teacher,
Core_Values=coredescription,
Marking=m,
Students_Enrollment_Records=students,
grading_Period=coreperiod,
)
V_insert_data.save()
this is the data i inputted
this is the result i received in my admin site
Update
I am selecting a mark for every student and every core value then sending all data back to the server
If i want to input like this
the result inserted in my database is like this
UPDATE AGAIN when i try to add this to my views
for markingID in request.POST.getlist('marking'):
marking.append(markingID)
print(marking[m])
the error says
list indices must be integers or slices, not str

try this
marking = []
for markingID in request.POST.getlist('Marking'):
marking.append(markingID)
for i, mark in enumerate(request.POST.getlist('Marking')):
s = StudentBehaviorMarking(id=mark)
core = corevalues[i]
cores = EducationLevelGradingBehavior(id=core)
print("mark", mark, "per description", core)
for student in request.POST.getlist('student'):
students = StudentPeriodSummary(id=student)
V_insert_data = StudentsBehaviorGrades(
Teacher=teacher,
# Education_Levels
Students_Enrollment_Records=students,
Grading_Period=coreperiod,
Grading_Behavior=cores,
Marking=s,
)
V_insert_data.save()

Just modify the code in below format
for m, students, desc in zip(request.Post.getlist('marking'), request.Post.getlist('student'), request.Post.getlist('coredescription')):
V_insert_data = StudentsCoreValuesDescription(
Teacher=teacher,
Core_Values=coredescription,
Marking=m,
Students_Enrollment_Records=students,
grading_Period=coreperiod,
)
V_insert_data.save()
return render(request, "Homepage/updatebehavior.html")

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>

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

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.

how to iterate over a list of variable length in django template?

I have a list of values like:
list_of_values = ['clients':['add, view'], 'vendors': ['add', 'delete', 'change'], 'companies': ['add', 'view', 'delete', 'change']]
Using django template tags I have to make a template like:
Activities ADD | VIEW | CHANGE | DELETE
clients checkbox checkbox checkbox checkbox
vendors checkbox checkbox checkbox checkbox
companies checkbox checkbox checkbox checkbox
Kindly let me know how can I achieve this?
List of values looks more like a dictionary to me, assuming it is:
<table>
<tr>
<th>Activities</th>
<th>ADD</th>
<th>VIEW</th>
<th>CHANGE</th>
<th>DELETE</th>
</tr>
{% for key in list_of_values %}
<tr>
<td>{{ key }}</td>
<td>
{% if 'add' in list_of_values.key %}
<input type="checkbox" checked/>
{% else %}
<input type="checkbox"/>
{% endif %}
</td>
<td>
{% if 'view' in list_of_values.key %}
<input type="checkbox" checked/>
{% else %}
<input type="checkbox"/>
{% endif %}
</td>
<td>
{% if 'change' in list_of_values.key %}
<input type="checkbox" checked/>
{% else %}
<input type="checkbox"/>
{% endif %}
</td>
<td>
{% if 'delete' in list_of_values.key %}
<input type="checkbox" checked/>
{% else %}
<input type="checkbox"/>
{% endif %}
</td>
</tr>
{% endfor %}
</table>