LogIn Class Based View

229 views
Skip to first unread message

dtdave

unread,
May 26, 2022, 3:37:32 PM5/26/22
to Django users
I have this function based view which works perfectly but I would like to convert to a class based view but am stuck on how to do this:
This is my fbv:

def loginView(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            user = authenticate(request,
                                   email=cd['email'],
                                   password=cd['password'])
       
            if user is not None:
                login(request, user)
                if user.is_authenticated and user.is_client:
                    return redirect('clients:detail') # Go to client dashboard
                elif user.is_authenticated and user.is_account_manager:
                    return redirect('accountmanagers:detail') #Go to account manager dashboard
            else:
                   return HttpResponse('Invalid login')
    else:
            # Invalid email or password. Handle as you wish
        form = LoginForm()

    return render(request, 'registration/login.html', {'form': form})

I would appreciate any help.

Vishesh Mangla

unread,
May 26, 2022, 4:57:41 PM5/26/22
to django...@googlegroups.com
I think it's good, function based views offer you more flexibility. If you are not liking your code it's because your function should be split up into smaller functions, or use a class without it being a view.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ea6c54f2-ecc0-407d-a296-e168d509a299n%40googlegroups.com.

Vishesh Mangla

unread,
May 26, 2022, 5:11:24 PM5/26/22
to django...@googlegroups.com

Vishesh Mangla

unread,
May 26, 2022, 5:13:16 PM5/26/22
to django...@googlegroups.com

Vishesh Mangla

unread,
May 26, 2022, 5:13:46 PM5/26/22
to django...@googlegroups.com

Lalit Suthar

unread,
May 27, 2022, 4:21:11 AM5/27/22
to django...@googlegroups.com

waqar khan

unread,
May 28, 2022, 3:02:36 PM5/28/22
to django...@googlegroups.com
I think so problem cleaned data try if this pattern
em= form.cleaned_data['email']
ps= form.cleaned_data['password']
user = authenticate(request,email=em,password=ps)

this is way not solve problem share me error

--
Reply all
Reply to author
Forward
0 new messages