Home > Software engineering >  request.session['pk'] = user.pk AttributeError: 'QuerySet' object has no attribu
request.session['pk'] = user.pk AttributeError: 'QuerySet' object has no attribu

Time:11-04

I need to get a user for account_verify to compare the input verification code with the correct login code, but I got this error also I want to show user phone number in account verify I used context for that but I think that is wrong

view

def account_register(request):
if request.user.is_authenticated:
    return redirect("store:home")
if request.method == "POST":
    registerForm = RegistrationForm(request.POST)
    if registerForm.is_valid():
        username = registerForm.cleaned_data["user_name"]
        phone_number = registerForm.cleaned_data["phone_number"]
        password = registerForm.cleaned_data["password"]
        user = User.object.filter(phone_number=phone_number)
        if not user.exists():
            new_user = User(phone_number=phone_number)
            new_user.username = username
            new_user.phone_number = phone_number
            new_user.set_password(password)
            new_user.is_active = False
            new_user.save()

        else:
            return HttpResponse("this phone number already taken", status=400)

        request.session['pk'] = user.pk
        return redirect("account:account_verify")
else:
    registerForm = RegistrationForm()
return render(request, "account/authentication.html", {"form": registerForm})


def account_verify(request):
form = CodeForm(request.POST or None)
print(form)
pk = request.session.get('pk')
print(pk)
if pk:
    user = User.object.get(pk=pk)
    code = user.code
    user_phone_number = user.phone_number
    code_user = f"{user.code}"
    if not request.POST:
        print(code_user)
        send_sms(code_user, user.phone_number)
    if form.is_valid():
        num = form.cleaned_data.get('number')

        if str(code) == num:
            user.is_active = True
            user.save()
            login(request, user)
            return redirect("store:home")
        else:
            return redirect("account:register")
context = {
    'form': form,
    'user_phone_number': user_phone_number
}
return render(request, "account/verify.html", context)

error

File "C:\Users\_rickoutis_\djx\Rex_acs32\account\views.py", line 49, in account_register
request.session['pk'] = user.pk

AttributeError: 'QuerySet' object has no attribute 'pk'

CodePudding user response:

regarding your problem: When you call:

user = User.object.filter(phone_number=phone_number)

You are get a QuerySet object. QuerySet have no access to attribute. If you want to use phone_number of specific user try replace this line of code with this one:

user = User.object.get(phone_number=phone_number)

By using 'get' statement instead of 'filter' you get access to specific record in database which allow you to use their attribute.

CodePudding user response:

the error in the below line:

user = User.object.filter(phone_number=phone_number)

filter method returns list of objects, so its not object type, to solve this issue use get() instead, the modified line is:

user = User.object.get(phone_number=phone_number)

CodePudding user response:

Use filter().first() to avoid this issue also if its not unique in your models would be good to make it like this

user = User.objects.filter(phone_number=phone_number).first()

so you would prevent MultipleObjectsReturned error.

but I would recomend you to make that phone_number unique=True

  • Related