Reputation: 145
I have two model class as Shopkeeper and Customer which have onetoone key from User Model. Now I have a view as
@require_customer()
def add_to_wishlist(request, pk):
product = Product.objects.get(pk=pk)
customer = Customer.objects.get(user=request.user)
wl = WishListProduct(product=product, customer=customer)
wl.save()
return HttpResponse("Added to Wish List !")
And the Decorator is as follow:
def require_customer(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url='/login/'):
def is_customer(u):
return Customer.objects.filter(user=u).exists()
actual_decorator = user_passes_test(lambda u: u.is_authenticated and is_customer, login_url=login_url,
redirect_field_name=redirect_field_name)
if function:
return actual_decorator(function)
else:
return actual_decorator
Now if I am logged in as Shopkeeper and I call this view with following url:
path('products/<pk>/addToCart/', views.add_to_cart, name='add_to_cart'),
It should redirect to Login page but instead it give me an error as
OnlineShops.models.DoesNotExist: Customer matching query does not exist.
Can you help me out finding the bug here?
Upvotes: 0
Views: 368
Reputation: 2817
I guess your lambda function
lambda u: u.is_authenticated and is_customer
should look like
lambda u: u.is_authenticated and is_customer(u)
This typo might allow not authenticated users to get into your view instead of redirecting them to Login page.
If it doesn't solve the problem - please give us the whole traceback, not only the exception text.
Upvotes: 1