peya02
January 30, 2023, 7:22am
1
Can you help me with this error?
A few days ago I was able to edit using the same form as to create but for a couple of days it hasn’t worked for me, there’s something different that doesn’t work. I get the following error:
view.py
def editar(request,id_reclamacion):
reclamacion2 = rec.objects.get(id=id_reclamacion)
formulario = RecForm(request.POST or None, request.FILES or None, instance=reclamacion2)
return render(request,'reclamaciones/editar.html', {'formulario': formulario})
ursl.py
urlpatterns = [
path('', Inicio.as_view(), name='home'),
path('reclamaciones/', Reclamaciones, name='Reclamaciones'),
path('logout/', exit, name='exit'),
path('crear/', crear, name='crear'),
path('reclamaciones/editar/<int:id_reclamacion>', editar, name='editar'),
The error appears when I go to the page to show all the records, where I have added a button, with the following code so that it knows what id it has. If I remove the code {% url ‘edit’ reclamation2.id %}, I already get the records but then it won’t edit me.
<a name="" id="" class="btn btn-info" href="{% url 'editar' reclamacion2.id %}" role="button">Editar</a>
Thank you.
That is impossible to work that way:
<a name="" id="" class="btn btn-info" href="{% url 'editar' reclamacion2.id %}" role="button">Editar</a>
You are using reclamacion
as an object, and you should use reclamacion
for edition, add this to your template:
<a name="" id="" class="btn btn-info" href="{% url 'editar' reclamacion.id %}" role="button">Editar</a>
peya02
January 30, 2023, 7:50am
3
still the same error. In my model I don’t have id but id_reclamation, so in this part of the code I give it id_reclamation
it is right?
def editar(request,id_reclamacion):
reclamacion2 = rec.objects.get(id=id_reclamacion)
formulario = RecForm(request.POST or None, request.FILES or None, instance=reclamacion2)
return render(request,'reclamaciones/editar.html', {'formulario': formulario})
models.py
class rec(models.Model):
id_reclamacion = models.AutoField(primary_key=True)
estado = models.IntegerField(verbose_name='estado', null=True)
fecha_reclamacion = models.DateField(verbose_name='Fecha Reclamacion', null=True)
fecha_resolucion = models.DateField(verbose_name='Fecha_resolucion', null=True)
num_reclamacion = models.CharField(max_length=100, verbose_name='N', null=True)
Which arguments are href=“{% url ‘edit’ reclamacion.id %}”
I mean, reclamation. id where does it come from? I understand that from views.py
Try this method:
def editar(request,id_reclamacion):
reclamacion = rec.objects.get(id=id_reclamacion)
formulario = RecForm(request.POST or None, request.FILES or None, instance=reclamacion2)
return render(request,'reclamaciones/editar.html', {'formulario': formulario, 'reclamacio': reclamacio})
And add id_reclamacio
to your template:
<a name="" id="" class="btn btn-info" href="{% url 'editar' reclamacion.id_reclamacio %}" role="button">Editar</a>
If this is not working Try using UpdateView
peya02
January 30, 2023, 8:57am
5
I keep getting the same error
It’s like it doesn’t pass the id correctly because if I remove the part of the code {% url ‘editar’ reclamacio.id_reclamacion %} then it lists the records, although it’s true that I can’t edit them.
I highly recommend you to use UpdateView
peya02
January 30, 2023, 10:41am
7
I have read about what you say although I have not implemented it. What is still curious is because if I remove the code
{% url ‘editar’ reclamacio.id %}
the record view works and also if I manually write the url http://127.0.0.1:8000/reclamaciones/editar/51
I also see the record with id 51 along with the form to modify.
I don’t understand…
peya02 :
if I manually write the url http://127.0.0.1:8000/reclamaciones/editar/51
I also see the record with id 51 along with the form to modify.
Answer :
That is why i recommend you to use UpdateView
to edit your existing objects. Do you really wanted to type this: http://127.0.0.1:8000/reclamaciones/editar/51
just to edit your model objects?
peya02 :
What is still curious is because if I remove the code
{% url ‘editar’ reclamacio.id %}
the record view works.
Answer :
You are seeing this error because of the wrong url.
Editing it using UpdateView
:
class rec(models.Model):
id_reclamacion = models.AutoField(primary_key=True)
estado = models.IntegerField(verbose_name='estado', null=True)
fecha_reclamacion = models.DateField(verbose_name='Fecha Reclamacion', null=True)
fecha_resolucion = models.DateField(verbose_name='Fecha_resolucion', null=True)
num_reclamacion = models.CharField(max_length=100, verbose_name='N', null=True)
slug = models.SlugField(unique = True, blank = True)
Views.py :
from django.views.generic.edit import UpdateView
Class EditRec(UpdateView):
model = Rec
fields = ['num_reclamacion', 'fecha_resolucion', 'fecha_reclamacion', '
estado', ]
template_name = 'edit_rec.html'
success_url = reverse_lazy('Dashboard')
edit_rec.html :
<form method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update">
</form>
urls.py :
path('edit/<slug:slug>/'), views.EditRec.as_view(), name= 'Edit')
Template :
<a name="" id="" class="btn btn-info" href="{% url 'Edit' reclamacion.slug %}" role="button">Editar</a>
I hope it helps.
Your original error is caused by this line:
You’re trying to reverse a url that needs a parameter. You’re trying to pass the parameter using an object named reclamacion2
.
However,
You’re not including relclamacion2
in the context for it to be rendered.
You either need to add that object to your context, or you need to retrieve that id from the form instance.
(None of these other changes regarding the type of view, etc, are necessary.)
peya02
January 30, 2023, 12:09pm
11
correct, as the parameter is passed it gives the error, that’s why when I remove the code {% url ‘editar’ reclamacion2.id %} the view works.
I remember that a month ago it worked for me, I don’t remember creating more code to edit a record.
What would be missing in this line?
return render(request,‘reclamaciones/editar.html’, {‘formulario’: formulario})
peya02
January 30, 2023, 12:10pm
12
Making the proposed view changes, I still get the same error from the beginning.
You would need to add reclamacion2
as a key and value to the context dict in the same style that you have formulario
in that dict.
1 Like
peya02
January 30, 2023, 12:28pm
14
I with the typical startup application create
Views.py
def editar(request, id):
libro = Libro.objects.get(id=id)
formulario = Libroform(request.POST or None, request.FILES or None, instance=libro)
if formulario.is_valid() and request.POST:
formulario.save()
return redirect('libros')
return render(request,'libros/editar.html', {'formulario': formulario})
urls.py
path('libros/editar/<int:id>', views.editar, name='editar'),
editar.html
{% extends "base.html" %}
{% block titulo %} Editar libro {% endblock %}
{% block contenido %}
<div class="card">
<div class="card-header">
Editar informacion del libro
</div>
<div class="card-body">
<h4 class="card-title">Datos del libro</h4>
{% include 'libros/form.html' %}
</div>
<div class="card-footer text-muted">
</div>
</div>
{% endblock %}
And in index.html I added
<a name="" id="" class="btn btn-info" href="{% url 'editar' libro.id %}" role="button">Editar</a>
With that it already worked for me, it is now that it does not work for me doing exactly the same but with another model
Adding that line to index.html has nothing to do with you rendering editar.html.
The two situations have nothing to do with each other.
peya02
January 30, 2023, 12:35pm
16
Well, what I had was the typical page (index.html) where all the books come out and you could modify each book through a button.
That button was added with the comments:
<a name="" id="" class="btn btn-info" href="{% url 'editar' libro.id %}" role="button">Editar</a>
That button will edit that uses the same form as to create.
I don’t know exactly what you mean by the last comment.
Ok, to clarify things. What view is rendering the template identified by the error message? That is the view (and template) we need to be looking at.
Regarding my last comment, let’s try to not confuse the issue at hand by introducing other, unrelated topics. If there’s anything else you want to address, we’ll do so after this problem is solved.
peya02
January 30, 2023, 12:43pm
18
The error comes up when I go to reclamaciones.html. Page where I show all the records of the model
{% extends "reclamaciones/base.html" %}
{% block contenido %}
<div class="card">
<div class="card-header">
Lista Reclamaciones
</div>
<div class="card-body">
<a name="" id="" class="btn btn-success" href="{% url 'crear' %}" role="button">Nueva Reclamacion</a>
<div class="table-responsive">
</br>
<table class="table table-primary">
<thead>
<tr>
<th scope="col">Codigo</th>
<th scope="col">Motivo</th>
<th scope="col">Fecha Creacion</th>
<th scope="col">Debe aprobar</th>
<th scope="col">Origen</th>
<th scope="col">Estado</th>
<th scope="col">Acciones</th>
</tr>
</thead>
<tbody>
{% for reclamaciones in Reclamaciones %}
<tr class="">
<td>{{ reclamaciones.num_reclamacion }}</td>
<td>{{ reclamaciones.motivo }}</td>
<td>{{ reclamaciones.fecha_reclamacion }}</td>
<td>{{ reclamaciones.aprueba }}</td>
<td>{{ reclamaciones.delegacion_departamento }}</td>
<td>{{ reclamaciones.estado }}</td>
<td>
<a name="" id="" class="btn btn-info" href="{% url 'editarrec' rec.id %}" role="button">Editar</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<div class="card-footer text-muted">
</div>
</div>
{% endblock %}
So what is the view that renders this template? (And is this the version of the template throwing the error?
peya02
January 30, 2023, 12:47pm
20
def Reclamaciones(request):
Reclamaciones = Rec.objects.all()
return render(request,'reclamaciones/Reclamaciones.html', {'Reclamaciones': Reclamaciones})
This view works correctly if I remove the code part of the button:
href="{% url 'editarrec' rec.id %}"