Issue
reverse_url is workin fine with a url that has-no/int:pk but does not work with a url that has /int:pk throws an error NoReverseMatch: Reverse for 'read_bty' with no arguments not found. 1 patterns tried:['read_bty/(?P[0-9]+)$']. The first (class=HBTYIndex) lists all customers created from the (class=HBTYCreateView) and the (class=HBTYReadView) displays the customers order records, the last (class=HBTYOrderView) is supposed to create an order and reverse_lazy to the url 'read_bty' but it keeps on throwing the above error when creating an order. Tried to change from int:pk to int:id still getting the same error. if i change the reverse_lazy to point to a url with no int:pk the record gets added and i get redirected to that page instead of staying on the same page and showing the new added record. Thank You All In Advance Much Appreciate The Knowledge.
Views.py
class HBTYIndex(generic.ListView):
model = HbtyCustomers
context_object_name = 'bty'
paginate_by = 100
template_name = 'accounts/modals/bty/clientindex.html'
ordering = ['-id']
success_url = reverse_lazy('btylist')
def get_queryset(self):
qs = self.model.objects.all()
p_f = CustomerFilter(self.request.GET, queryset=qs)
return p_f.qs
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['filter'] = CustomerFilter(self.request.GET, queryset=self.get_queryset())
return context
# Create Customer
class HBTYCreateView(BSModalCreateView):
template_name = 'accounts/modals/bty/create_hbty.html'
form_class = btyForm
success_message = 'Success: Client was created.'
success_url = reverse_lazy('btylist')
# View Customer Orders History
class HBTYReadView(generic.ListView):
model = HbtyOrder
context_object_name = 'bty'
template_name = 'accounts/modals/bty/read_hbty.html'
allow_empty = False
pk_url_kwargs = 'hbtycustomer_pk'
paginate_by = 100
ordering = ['-id']
success_url = reverse_lazy('read_bty')
def get_queryset(self):
qs = self.model.objects.filter(hbtycustomer_id=self.kwargs['pk'])
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
# Create New Order in the customer history page
class HBTYOrderView(BSModalCreateView):
template_name = 'accounts/modals/bty/create_hbty.html'
form_class = HairbtyOrderForm
success_message = 'Success: Order was created.'
success_url = reverse_lazy('read_bty')
read_hbty.html
<div class="row">
<div class="col-12 mb-3">
{% if filter.qs %}
{% include "accounts/modals/hairbty/vw_more.html" %}
{% else %}
<p class="no-books text-primary">No Client addeds yet.</p>
{% endif %}
</div>
</div>
Models.py
class HbtyCustomers(models.Model):
name = models.CharField(max_length=200, blank=False, null=True)
address = models.CharField(max_length=200, blank=False, null=True)
date = models.IntegerField(blank=False, null=True)
remarks = models.CharField(max_length=200, blank=False, null=True)
def __str__(self):
return self.name
class HbtyCategories(models.Model):
name = models.CharField(max_length=30)
def __str__(self):
return self.name
class HbtySubcategories(models.Model):
categ = models.ForeignKey(HbtyCategories, on_delete=models.CASCADE)
name = models.CharField(max_length=30)
def __str__(self):
return self.name
class HbtyOrder(models.Model):
STATUS = (
('Pending', 'Pending'),
('Out for delivery', 'Out for delivery'),
('Delivered', 'Delivered'),
)
categ = models.ForeignKey(HbtyCategories, on_delete=models.SET_NULL, blank=True, null=True)
subcateg = models.ForeignKey(HbtySubcategories, on_delete=models.SET_NULL, blank=True, null=True)
hbtycustomer = models.ForeignKey(HbtyCustomers, on_delete=models.SET_NULL, blank=True, null=True)
price = models.IntegerField(null=True)
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
status = models.CharField(max_length=200, null=True, choices=STATUS)
def __str__(self):
return str(self.id)
Urls.py
path('btylist/', views.HBTYIndex.as_view(), name="btylist"),
path('create_btycustomer/', views.HBTYCreateView.as_view(), name='create_btycustomer'),
path('create_btyorder/', views.HBTYOrderView.as_view(), name='create_btyorder'),
path('read_bty/<int:pk>', views.HBTYReadView.as_view(), name='read_bty'),
Thank You All.
Solution
As you can tell from your urls.py, read_bty/HBTYReadView wants to see an int value named pk.
When you call that url in HBTYOrderView via reverse_lazy, you don't provide it, hence the error.
You can build out the success_url by creating a get_success_url method in your HBTYOrderView, rather than using a success_url property, something like:
def get_success_url(self):
return reverse_lazy('read_bty',kwargs={"pk": self.request.user.id} )
(I am assuming here that the ID that read_bty wants is the request.user.id )
Answered By - SamSparx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.