Django Class-Based Views - Noreversematch Error

Asked

Viewed 97 times

0

I’m developing a CRUD system with class-based views. I have an app that List, Create and Delete views are working perfectly, but Update is having a problem. From Listview, I try to Updateview one of the objects, but the object data is not passed to Updateview, generating the error:

NoReverseMatch at /workOrder/8/
Reverse for 'WorkOrderUpdate' with arguments '('',)' not found. 1 pattern(s) tried: ['workOrder/(?P<pk>[0-9]+)/$']

workOrderList.html - Here the objects are shown correctly.

{% for workOrder in object_list %}
  <tr>
    <td>{{ workOrder.id }}</td>
    <td>
      <a href="{% url 'WorkOrderUpdate' workOrder.id %}">
        <button type="button" class="btn btn-outline-info">Editar</button>
      </a>
    </td>
    ~ Botão de delete omitido ~                   
  </tr>
{% endfor %}

urls.py - My four urls, all working except Update.

path('workOrder/add/', WorkOrderCreate.as_view(), name='WorkOrderCreate'),
path('workOrders', WorkOrderList.as_view(), name='WorkOrderList'),
path('workOrder/<int:pk>/', WorkOrderUpdate.as_view(), name='WorkOrderUpdate'),
path('workOrder/<int:pk>', WorkOrderDelete.as_view(), name='WorkOrderDelete'),

workOrderUpdate.html - Here my form that should receive the id passed by Listview.

<form action="{% url 'WorkOrderUpdate' workOrder.id %}" method='post'>
{% csrf_token %}

If I change to {% url 'WorkOrderUpdate' 8 %}, the form is displayed perfectly (8 is one of the id present in my table).

views.py - Exactly like the views of other apps that are working (structurally speaking).

class WorkOrderUpdate(UpdateView):
    model = WorkOrder
    form_class = WorkOrderForm
    template_name = 'workOrders/workOrderUpdate.html'
    success_url = '/workOrders'

Forms.py - Exactly like the other apps that are working (structurally speaking).

class WorkOrderForm(forms.ModelForm):

    class Meta:
        model = WorkOrder
        fields = ['customer', 'status']


    customer = forms.ModelChoiceField(empty_label="Selecione", queryset=Customer.objects.all())
    createdAt = forms.DateField(label="Criado em", input_formats=settings.DATE_INPUT_FORMATS)
    status = forms.CharField(label="Status")

I have two other apps extremely similar to this, and they have all the views working correctly. Would anyone have any idea why my id is not passed to Updateview?

  • Hello, Denis. The problem is that probably the workOrder.id is returning None. Could you please share your view? @Denis Callau

  • Added views.py and Forms.py

1 answer

1


Add the object currently being edited to the context with the desired name. Add this method to your class that you inherit from UpdateView.

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['workOrder'] = self.object
    return context
  • It worked! But now because in this app I needed this method and the other two didn’t? They’re all the same, just change the models practically.

  • It’s hard to say for sure without testing the other apps, but the object name is set in a property called context_object_name, class Singleobjectmixin.@Denis Callau.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.