Home > Net >  Class Based View Reverse URL not reversing to URL with id
Class Based View Reverse URL not reversing to URL with id

Time:10-05

I have different views trying to add a product for one member created on the app not a user with authentication more of a one admin management system. Below is my code kindly help much appreciated.

I get an error:

django.urls.exceptions.NoReverseMatch: Reverse for 'read_hairbty' with no arguments not found. 1 pattern(s) tried: ['read_hairbty/(?P[0-9] )$']

Views.py

Create Appointment

class HBTYOrderView(BSModalCreateView):
    model = HbtyOrder
    template_name = 'accounts/modals/hairbty/create_hbtyordr.html'
    form_class = HairbtyOrderForm
    success_message = 'Success: Appointment was created.'
    success_url = reverse_lazy('read_hairbty')

View Appointment

class HBTYReadView(generic.ListView):
    model = HbtyOrder
    context_object_name = 'hairbty'
    template_name = 'accounts/modals/hairbty/read_hbty.html'
    allow_empty = True
    pk_url_kwargs = 'hbtycustomer_id'
    paginate_by = 100
    
    def get_queryset(self):
        qs = self.model.objects.filter(hbtycustomer_id=self.kwargs['pk']).order_by('-date_created')
        p_f = HbtyOrdersFilter(self.request.GET, queryset=qs)
        return p_f.qs
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['filter'] = HbtyOrdersFilter(self.request.GET, queryset=self.get_queryset())
        return context

Urls.py

path('read_hairbty/<int:pk>', views.HBTYReadView.as_view(), name='read_hairbty'),

path('create_hairbtyorder/', views.HBTYOrderView.as_view(), name='create_hairbtyorder'),

CodePudding user response:

You can work with get_success_url().

class HBTYOrderView(BSModalCreateView):
    model = HbtyOrder
    template_name = 'accounts/modals/hairbty/create_hbtyordr.html'
    form_class = HairbtyOrderForm
    success_message = 'Success: Appointment was created.'
    
    def get_success_url(self):
        return reverse('read_hairbty', kwargs={'pk': self.object.pk})

CodePudding user response:

Try this:

class HBTYOrderView(BSModalCreateView):
    ...

    def get_success_url(self):
        return reverse('read_hairbty', kwargs={'pk': self.object.hbtycustomer_id})
  • Related