Home > Blockchain >  How can I show an error message If the username already exists for creating an account?
How can I show an error message If the username already exists for creating an account?

Time:03-01

When I go to create a new account by the same user name that already exists in the database, then shows the below error. I just want to show an error message (the user already exists, try again with a unique username) if the users exist. How can I fix these issues? What will be the condition?

Problem:

IntegrityError at /singup
UNIQUE constraint failed: auth_user.username
Request Method: POST
Request URL:    http://127.0.0.1:8000/singup
Django Version: 3.2.3
Exception Type: IntegrityError
Exception Value:    
UNIQUE constraint failed: auth_user.username
Exception Location: C:\Users\DCL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py, line 423, in execute
Python Executable:  C:\Users\DCL\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.5
Python Path:    
['D:\\1_WebDevelopment\\Business_Website',
 'C:\\Users\\DCL\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
 'C:\\Users\\DCL\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
 'C:\\Users\\DCL\\AppData\\Local\\Programs\\Python\\Python39\\lib',
 'C:\\Users\\DCL\\AppData\\Local\\Programs\\Python\\Python39',
 'C:\\Users\\DCL\\AppData\\Roaming\\Python\\Python39\\site-packages',
 'C:\\Users\\DCL\\AppData\\Roaming\\Python\\Python39\\site-packages\\win32',
 'C:\\Users\\DCL\\AppData\\Roaming\\Python\\Python39\\site-packages\\win32\\lib',
 'C:\\Users\\DCL\\AppData\\Roaming\\Python\\Python39\\site-packages\\Pythonwin',
 'C:\\Users\\DCL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
Server time:    Mon, 28 Feb 2022 18:15:25  0000

views.handle_singUp:

def handle_singUp(request):
    if request.method == "POST":
        userName = request.POST['username']
        fname = request.POST['fname']
        lname = request.POST['lname']
        email = request.POST['email']
        pass1 = request.POST['pass1']
        #pass2 = request.POST['pass2']

        myUser = User.objects.create_user(userName,email,pass1)
        myUser.first_name = fname
        myUser.last_name = lname

        myUser.save()
        messages.success(request,"Your account has been created!")
        return redirect("/") 
                
    else:
        return HttpResponse("404-Not found the page")

urls.py:

urlpatterns = [
    path('', views.index, name="index"),
    path('singup', views.handle_singUp, name= "handle_singUp"),
    path('login', views.handle_login, name="handle_login"),
    path('logout', views.handle_logout, name="handle_logout"),
    path('contact', views.handle_contact, name="handle_contact"),
    path('frontend_orders', views.frontend_orders, name="frontend_orders"),
    path('hire_me', views.hire_me, name="hire_me")

]

CodePudding user response:

You can use Error Handling.

def handle_singUp(request):
    if request.method == "POST":
        userName = request.POST['username']
        fname = request.POST['fname']
        lname = request.POST['lname']
        email = request.POST['email']
        pass1 = request.POST['pass1']
        #pass2 = request.POST['pass2']

        myUser = User.objects.create_user(userName,email,pass1)
        myUser.first_name = fname
        myUser.last_name = lname
        try:
            myUser.save()
            messages.success(request,"Your account has been created!")
            return redirect("/") 
        except:
            messages.success(request,"The username is already exist!")
            return redirect("/") 

        return redirect("/") 
            
    else:
        return HttpResponse("404-Not found the page")

CodePudding user response:

I'll suggest you to use Forms [Django-doc] and for current problem you've to add check for username like this

def handle_singUp(request):
    if request.method == "POST":
        userName = request.POST['username']
        if User.objects.filter(username=username).exists():
           myUser = User.objects.create_user(userName,email,pass1)
           myUser.first_name = fname
           myUser.last_name = lname

           myUser.save()
           messages.success(request,"Your account has been created!")
           return redirect("/") 
        else:
           messages.error(request,"User already exists") # your error message
           return redirect(request.path)
                
    else:
        return HttpResponse("404-Not found the page")
  • Related