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

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

Related

Django inline formset not valid on update

My create method works perfectly, but the update does not. It returns one error for each inline as an empty list []
forms.py
StructureFormset = inlineformset_factory(Plan, Structure,
fields = (
'material_type',
'weight',
'thickness',
'provider'
),
extra=0,
can_delete=True
)
TestFormset = inlineformset_factory(Plan, Test,
fields=(
'essay',
'critic',
'spec'
),
extra = 0,
can_delete=True
)
class PlanForm(ModelForm):
class Meta:
model = Plan
fields = (
'format',
'pc',
'revission',
'rev_date',
'client',
'product',
'gp_code',
'code',
'observation',
'continuation',
'dispatch_conditions',
'elaborator',
'reviewer'
)
localized_fields=('rev_date',)
views.py
def editPlan(request, pk):
plan = Plan.objects.get(id = pk)
form = PlanForm(instance = plan)
sformset = StructureFormset(instance=plan)
tformset = TestFormset(instance=plan)
context = {
'form': form,
'sformset': sformset,
'tformset': tformset
}
if request.method == 'POST':
print('----------------------------request----------------------------')
print(request.POST)
form = PlanForm(request.POST, instance = plan)
sformset = StructureFormset(request.POST, instance=plan, prefix='structure')
tformset = TestFormset(request.POST, instance=plan, prefix='test')
if form.is_valid() and sformset.is_valid() and tformset.is_valid():
print('-----------------------is valid-----------------------')
form.save()
instances = sformset.save(commit=False)
for instance in instances:
sformset.save_m2m()
instance.save()
tformset.save()
return redirect('/plans/')
else:#printing errors
print('-----------------------is not valid-----------------------')
print(sformset.total_error_count())
print(sformset.errors)
print(tformset.total_error_count())
print(tformset.errors)
return render(request, 'home/plan_forms.html', context)
template.html
{{ tformset.management_form }}
<table>
<thead>
<tr>
<th>Essay</th>
<th>
<abb title="Critical Variable">Crit.</abb>
</th>
<th>Spec</th>
{% if tformset.instance.pk %}
<th class="text-center px-1 col-1 pb-2">
<i class="material-icons">delete</i>
</th>
{% endif %}
</tr>
</thead>
<tbody id="tform_set">
<!--Test form goes here-->
{% if tformset %}
{% for tt in tformset %}
{% for hidden in tt.hidden_fields %}
{{ hidden }}
{% endfor %}
<tr>
<td>
{{ tt.essay }}
</td>
<td class="px-1 col-1 text-center justify-content-center">
{{ tt.critic }}
</td>
<td>
{{ tt.spec }}
</td>
<td>
{% if tt.instance.pk %}{{ tt.DELETE }}{% endif %}
</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
<tbody id="tempty_form" style="display:none !important;">
<tr>
<td>
{{ tformset.empty_form.essay }}
</td>
<td>
{{ tformset.empty_form.critic }}
</td>
<td>
{{ tformset.empty_form.spec }}
</td>
</tr>
</tbody>
</table>
{{ sformset.management_form }}
<table>
<thead>
<tr>
<th>Material</th>
<th>Weight (g/m²)</th>
<th>Thickness (μ)</th>
<th>Provider</th>
{% if sformset.instance.pk %}
<th class="text-center col-1 px-1 pb-2">
<i class="material-icons">delete</i>
</th>
{% endif %}
</tr>
</thead>
<tbody id="sform_set">
<!--Structure form goes here-->
{% if sformset %}
{% for ss in sformset %}
{% for hidden in ss.hidden_fields %}
{{ hidden }}
{% endfor %}
<tr>
<td>
{{ ss.material_type }}
</td>
<td>
{{ ss.weight }}
</td>
<td>
{{ ss.thickness }}
</td>
<td>
{{ ss.provider }}
</td>
<td>
{% if ss.instance.pk %}{{ ss.DELETE }}{% endif %}
</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
<tbody id="sempty_form" style="display:none !important;">
<tr>
<td>
{{ sformset.empty_form.material_type }}
</td>
<td>
{{ sformset.empty_form.weight }}
</td>
<td>
{{ sformset.empty_form.thickness }}
</td>
<td>
{{ sformset.empty_form.provider }}
</div>
</td>
</tr>
</tbody>
</table>
This is what i get from the prints;
notice the:
`
1
[]
1
[]
` bellow the `is not valid`
[![Print Output](https://i.stack.imgur.com/whRc0.png)](https://i.stack.imgur.com/whRc0.png)

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

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>

Django Inserting data (Getting correct data)

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