Need help in User log in, someone please help

587 views
Skip to first unread message

Raj

unread,
Feb 22, 2022, 1:25:24 PM2/22/22
to Django users
I am trying to create register form access in django, but i am getting this " ValueError at /registerThe given username must be set" error.

Kindly help me how can I fix this bug.
please.

here is the code of the views.py file.--->
from msilib.schema import Feature
from pyexpat.errors import messages
from django.shortcuts import render, redirect
from django.contrib.auth.models import User, auth
from django.contrib import messages
from django.http import HttpResponse
from .models import Feature

# Create your views here.
def index(request):
    return render(request,'index.html',{'features': features})

#=-------register function-------
def register(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        email = request.POST.get('email')
        password = request.POST.get('password')
        password2 = request.POST.get('password2')
        # username = request.POST['username']
        # email = request.POST['email']
        # password = request.POST['password']
        # password2 = request.POST['password2']

        if password == password2:
            if User.objects.filter(email = email).exists():
                messages.info(request, 'Email already has been used!')
                return redirect('register')
            elif User.objects.filter(username= username).exists():
                messages.info(request, 'Username already exist')
                return redirect('register')
            else:
                user = User.objects.create_user(username=username, email=email, password = password)
                user.save();
                print("User created")
                return redirect('login')  #check
        else:
            messages.info(request, 'Incorrect password')
            return redirect('register')
    else:
        return render(request, 'register.html')

def login(request):
    if request.method == 'POST':
        # username = request.POST['username']
        # password = request.POST['password']
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = auth.authenticate(username = username, password = password)

        if user is not None:
            auth.login(request, user)
            return redirect('/')
        else:
            messages.info(request,'Credential Invalid')
            return redirect('login')

    else:
        return render(request, 'login.html')

here is the SS of the errorScreenshot (75).png

Antonis Christofides

unread,
Feb 23, 2022, 1:54:21 AM2/23/22
to django...@googlegroups.com, Raj

You have this statement:

    user = User.objects.create_user(username=username, email=email, password = password)

Just before this statement, you need to insert a statement that will enable you to examine the value of the "username" statement. Something like this:

    print(username)
    user = User.objects.create_user(username=username, email=email, password = password)

This might or not might work properly with Django. What most Python programmers would do instead is this:

    import pdb; pdb.set_trace()
    user = User.objects.create_user(username=username, email=email, password = password)

but for that you need to know how to use pdb. It's quite simple though tricky at first. Eventually you will need to learn it, however, so now would be a good time. Search the web.

Regards,

Antonis
Antonis Christofides
+30-6979924665 (mobile)
--
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/c7bcf4b5-f8e7-4f84-97df-02e4655e42f1n%40googlegroups.com.

Jitendra kumar Patra

unread,
Feb 23, 2022, 1:56:42 AM2/23/22
to django...@googlegroups.com, Raj

vijay chourey

unread,
Feb 23, 2022, 2:25:10 AM2/23/22
to django...@googlegroups.com
Hi Raj,

Error is showing due to the blank value in user name, please try to print username before registration if it's blank please check form fields. 

Once done you can directly login your account with 

"auth.login(request,user)


waqar khan

unread,
Feb 23, 2022, 9:14:28 AM2/23/22
to django...@googlegroups.com

Raj

unread,
Feb 23, 2022, 10:25:56 AM2/23/22
to Django users
Hi, 
Thanks for the response, I have tried to print the username before creating the user, but it did not work. I have also tried to use pdb that also didn't work.

How to do it can give proper instructions??
I have searched about it on google but have not received any relevant answer.

Mihir Patel

unread,
Jul 22, 2022, 4:25:43 AM7/22/22
to Django users
i am unable to authenticate normal user is anyone have idea, what problem could be?

views.py

def userlogin(request):
    if not request.user.is_authenticated:
        if request.method == "POST":
            fm = AuthenticationForm(request=request, data=request.POST)
            if fm.is_valid():
                username = fm.cleaned_data['username']
                password = fm.cleaned_data['password']
                user = authenticate(request,username=username, password=password)
                if user is not None:
                    login(request, user)
                    return HttpResponseRedirect('/jobseekersprofile/')
        else:
            fm = AuthenticationForm()
        return render(request, 'jobglobel/userlogin.html', {'form': fm})            
    else:
        if request.user.is_authenticated:
            return HttpResponseRedirect('/jobseekersprofile/')
    return render(request, 'jobglobel/userlogin.html', {'form': fm})

Reply all
Reply to author
Forward
0 new messages