Getting error in url from HTML template to urls. No reverse match - django

I am trying to delete a record the on basis of primary key, so I am trying to pass a PK to url into a view function but getting this error:
django.urls.exceptions.NoReverseMatch:
Reverse for 'delete_view' with arguments '('',)' not found. 1 pattern(s) tried: ['essay/(?P<essay_id>[0-9]+)/delEssay/$']
My html code:
<form method="POST" action="{% url 'essay:delete_view' teacher.id %}">
<div class="page-header">
<div class="span4">
<h1>Manage Essays</h1>
</div>
</div>
<table>
<tr>
<th>Title</th>
<th>Edit</th>
<th>Delete</th>
</tr>
{% csrf_token %}
{% for uploadassignment in teacher.uploadassignment_set.all %}
<tr>
<th for="uploadassignment{{ forloop.counter }}">{{uploadassignment.title}}</th>
<th><input type="submit" value="Edit" class="btn btn-primary"/></th>
<th><input type="submit" value="Delete" name="delEssay" id="delEssay" class="btn btn-primary"/></th>
</tr>
{% endfor %}
</table>
</form>
URL:
url(r'^(?P<essay_id>[0-9]+)/delEssay/$', views.delete_view, name='delete_view')
View Function:
def delete_view(request,essay_id):
print("Delete")
print(essay_id)
object = UploadAssignment.objects.get(id=essay_id)
object.delete()
return render(request, 'essay/THome.html')

You are trying to access the object ID teacher.id, instead you try with primary key pk like teacher.pk
I had the same problem and i replaced it with pk and it worked.
hope this solves your problem.

Related

HTMX form submission produces a duplicate form

{% extends "IntakeApp/base3.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block heading %}
<h2>Allergies for {{request.session.report_claimant}}</h2>
{% endblock %}
{% block content %}
<form hx-post="{% url 'allergy' %}" hx-target="#allergy_target" hx-swap="outerHTML">{% csrf_token %}
<div class="form-row">
<div class="form-group col-md-2 mb-0">
{{ form.allergen|as_crispy_field }}
</div>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
<div class="container-fluid">
<table class="table table-striped table-sm" id="med-table">
<thead>
<tr>
<th>Allergen</th>
</tr>
</thead>
<tbody id="allergy_target">
{% for allergy in allergy_list %}
<tr>
<td>{{allergy.allergen}}</td>
<td>
<form method="POST" action="{% url 'allergy-delete' allergy.id %}">{% csrf_token %}
<input class="btn btn-danger btn-sm" type="submit" value="Delete">
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
class AllergyCreateView(generic.CreateView):
model = Allergy
template_name = 'IntakeApp/allergy_form.html'
form_class = AllergyForm
def form_valid(self, form):
form.instance.assessment = Assessment.objects.get(id=self.request.session['assessment_id'])
return super(AllergyCreateView, self).form_valid(form)
def get_success_url(self):
return reverse_lazy("allergy")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
assessment_id = self.request.session['assessment_id']
allergy_list = Allergy.objects.filter(assessment=assessment_id)
context["allergy_list"] = allergy_list
return context
I tried to all the different hx-swap options, but none fix it...It does post correctly and the swap does work, just not sure why I am getting another form in there. Please help
I added the View above. I think thats were my issue is...not sure if I am supposed to be doing this way or not?
Seems like you're trying to render the same html content in your view, which instead should only take a specific element.
You can try to use hx-select="#allergy_target" so htmx will only fetch the table content.

Django shopping cart update

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.

got an unexpected keyword argument 'id'

I'm trying to do a query when the object has the status = 'Opened'. And display in a table where I will have a button to give a solution for my form and change status='Pending'. But when I click in the button I get this error.
What I'd like to do for really is display was a form for each data, but when I do a for loop for each one form my data insnt show, how you can see my editable.html. I just get the buttons to do an action, and they are working fine.
url:
path('manutencao_os_status/<int:id>', views.manutencao_ordem_de_servico, name='manutencao_os_status'),
path('manutencao_ordem_de_servico/', views.manutencao_ordem_de_servico, name='manutencao_ordem_de_servico'),
views.py
def manutencao_ordem_de_servico(request):
ordem_servico = OrdemServico.objects.filter(status='Aberto').order_by('id')
form = FormOrdemServico(ordem_servico)
if request.method != 'POST':
return render(request, 'ordemservico/manutencao_ordem_de_servico.html', {
'form': form,
'ordem_servico': ordem_servico
})
form = FormOrdemServico(request.POST, ordem_servico)
if not form.is_valid():
return render(request, 'ordemservico/manutencao_ordem_de_servico.html', {
'form': form,
'ordem_servico': ordem_servico
})
ordem_servico.save()
return redirect('ordemservico:manutencao_ordem_de_servico')
def manutencao_os_status(request, id):
ordem_servico = OrdemServico.objects.get(pk=id)
ordem_servico.status = 'Em Aprovação'
ordem_servico.save()
return redirect('ordemservico:manutencao_os_status')
html:
{%extends 'base.html' %}
{%block conteudo %}
<h1>Ordens de Serviço</h1>
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="card card-primary">
<div class="table table-bordered">
<table class="table table-bordered">
<thead>
<tr>
<td>Condition:</td>
<td>ID:</td>
<td>Name:</td>
<td>Status:</td>
<td>Solution:</td>
</tr>
</thead>
<tbody>
{% for os in ordem_servico %}
<tr>
<td>
<a href="{% url 'ordemservico:manutencao_os_status' os.id %}"
class="btn btn-success">Aprovar</a>
</td>
<td>{{os.id}}</td>
<td> {{os.name}}</td>
<td>{{os.status}}</td>
<td>{{os.solution}}</td>
</tr>
{%endfor%}
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
{% endblock %}
my editable.html:
{%for os in ordem_servico %}
<form action="{% url 'ordemservico:manutencao_os_status' os.id %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<fieldset>
<legend><h2>Ordens de Serviço</h2></legend>
<table class="table">
{{ os.form }}
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary">Solucionar</button>
</td>
</tr>
</table>
</fieldset>
</form>
{%endfor%}
{% endblock %}
You routed to the wrong view, it should be:
path(
'manutencao_os_status/<int:id>',
views.manutencao_os_status,
name='manutencao_os_status'
),
In the view you should likely redirect to the manutencao_ordem_de_servico view:
def manutencao_os_status(request, id):
ordem_servico = OrdemServico.objects.filter(pk=id).update(
status='Em Aprovação'
)
return redirect('ordemservico:manutencao_ordem_de_servico')
Note: A GET request is not supposed to have side-effects, hence updating
objects when a user makes a GET request, is not compliant with the HTTP
standard. Therefore it might be better to update a OrdemServico with a POST request.

Call two different urls from a form or input submit button Django

I have a form in which I have a table.In table I have a record(title).Now along with title I have two buttons delete and edit.Now I want is that, if I click edit it calls separate url along with primary key going through it or if I click delete It calls separate url with primary key going through.
here's my code:
<form method="POST" action="{% url 'essay:delete_view' teacher.id %}">
<div class="page-header">
<div class="span4">
<h1>Manage Essays</h1>
</div>
</div>
<table>
<tr>
<th>Title</th>
<th>Edit</th>
<th>Delete</th>
</tr>
{% csrf_token %}
{% for uploadassignment in teacher.uploadassignment_set.all %}
<tr>
<th for="uploadassignment{{ forloop.counter }}">{{uploadassignment.id}}</th>
<th><input type="submit" id="Editthis" value="{{uploadassignmentEdit.id}}" class="btn btn-primary"/></th>
<th><input type="submit" id="uploadassignment{{ forloop.counter }}" value="{{uploadassignment.id}}" name="uploadassignment" class="btn btn-primary"/></th>
</tr>
{% endfor %}
</table>
</form>
I want this: one url is called by below (Edit button) with teacher.id
<th><input type="submit" id="Editthis" value="{{uploadassignmentEdit.id}}" class="btn btn-primary"/></th>
And another url is called when hit delete button with teacher id.
<th><input type="submit" id="uploadassignment{{ forloop.counter }}" value="{{uploadassignment.id}}" name="uploadassignment" class="btn btn-primary"/></th>
So far due to action ="{% url 'essay:delete_view' teacher.id %}" in form I can switch to only one url.Kindly help in my code so I can switch to different urls with teacher id when clicked on delete or edit button.
urls.py:
url(r'^$', views.Indexview.as_view(), name='index'),
url(r'^login/$',views.loginform.as_view(),name='login'),
url(r'^addfile/$',views.Addfile.as_view(),name='file'), #essay/addfile
url(r'^addfile/$',views.EditEssay.as_view(),name='eEssay'),
url(r'^text/$',views.writetext.as_view(),name='text'),
url(r'^about/$',views.aboutus.as_view(),name='about'),
url(r'^stdprofile/$',views.studentprofile.as_view(),name='stdprofile'),
url(r'^logout/$', LogoutView.as_view(), name='user_logout'),
url(r'^(?P<teacher_id>[0-9]+)/delEssay/$', views.delete_view, name='delete_view'),

How to open a template as modal (detail user data) in user_list page (Django 1.11)

I have a user list page which works fine, and a user detail page which works fine too that I call from urls.py in seperate window. I want to open user detail in user list page in a modal window.
user_list.html
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th></th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr userid="{{user.id}}" class="edit_user">
<td>{{user.first_name}}</td>
<td>{{user.last_name }}</td>
<td>{{user.username }}</td>
<td>
<form class="right user_delete" method="POST" userid="{{user.id}}"
action="{% url 'user_delete' user.id%}">
{% csrf_token %}
<input class="btn btn-danger btn-sm" type="submit" value="DELETE">
</form>
</td>
<td><a type="button" class="btn btn-primary edit_user" href="{% url 'user_details' user.id %}" target="#edit_user"> UPDATE </a></td>
</tr>
{% empty %}
<tr>
<td>No Projects.</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#registerformmodal"> New user </button>
user_detail.html
<h1>User Details</h1>
<p>{{ user_details.username }}</p>
<p>{{ user_details.first_name }}</p>
views.py
def user_details(request, userid):
user = User.objects.get(id=userid)
template = loader.get_template('vts/user_detail.html')
context = {
'user_details': user,
}
return HttpResponse (template.render(context, request))
The button data-target here,
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#registerformmodal"> New user </button>
looks for #registerformmodal which is neither associated with <form> nor <div>
You can add the id to your form as this
<form id="#registerformmodal" class="right user_delete" method="POST" userid="{{user.id}}">
...
</form>