M
M
Mikhail Bolev2021-03-23 20:25:57
Django
Mikhail Bolev, 2021-03-23 20:25:57

Why are two exceptions thrown?

There is code in the view. If there is no such element in the model (in the database), then a standard exception should be thrown.

Entry.objects.get(id=-999) # raises Entry.DoesNotExist

But in practice, another exception is raised that there is no such field in the model and an error occurs:
type object 'Coupon' has no attribute 'DoesNotExists'

Why?

@require_POST
def coupon_apply(request):
    now = timezone.now()
    form = CouponApplyForm(request.POST)
    if form.is_valid():
        code = form.cleaned_data['code']
        try:
            coupon = Coupon.objects.get(code__iexact=code,
                                        valid_from__lte=now,
                                        valid_to__gte=now,
                                        active=True)
            request.session['coupon_id'] = coupon.id
        except Coupon.DoesNotExists:
            request.session['coupon_id'] = None
    return redirect('cart:cart_detail')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Bolev, 2021-03-23
@Scorpion_MB

Prompted. Typo :
DoesNotExist
DoesNotExists
Question closed.

D
Dr. Bacon, 2021-03-23
@bacon

What why? You are clearly written that Coupon does not have the DoesNotExists attribute

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question