Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")

]

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading