[Fixed]-Getting error in mixins when I try to login as a user

1👍

That’s because UserCheckout.objects.get(id=user_check_id) fails.

You should catch any potential fails like this:

try:
    user_checkout = UserCheckout.objects.get(id=user_check_id)
except UserCheckout.DoesNotExist as e:
    # do something if UserCheckout obj with user_check_id does not exist
else:
    # user_checkout succeeded. Procceed

or you can throw a 404 page if object does not exist

from django.shortcuts improt get_object_or_404

def get_queryset(self):
    user_check_id = self.request.user.id
    user_checkout = get_object_or_404(UserCheckout, id=user_check_id)
    return super(OrderList, self).get_queryset().filter(user=user_checkout)
👤nik_m

0👍

I would rather say that this was more of a logical error my bad 😐

In the above code I was trying to compare two quantities i.e. username id and email address id of user which led to that error. So I decided to make things simpler by using username and email address as a whole and then comparing it which worked for me

Edited
views.py

from django.shortcuts import render
from django.views.generic.edit import CreateView, FormView
from  django.views.generic.list import ListView
# Create your views here.
from .mixins import CartOrderMixin, LoginRequiredMixin
from .models import UserCheckout, Order

class OrderList(LoginRequiredMixin, ListView):
    queryset = Order.objects.all()

    def get_queryset(self):
        user_check_id = self.request.user.email
        template_name = 'orders/order_list.html'
        print user_check_id
        user_checkout = UserCheckout.objects.get(email=user_check_id)
        print user_checkout
        return super(OrderList, self).get_queryset().filter(user=user_checkout) 

Leave a comment