Home > Software engineering >  I am trying to create user in auth_user in Django but I am getting this error TypeError at /register
I am trying to create user in auth_user in Django but I am getting this error TypeError at /register

Time:10-26

Hello I am trying to create user in auth_user in database but it is showing me a error

The method I am using

from django.contrib.auth.models import User
user = Users.objects.get(username='myuser')
user.set_password('mypassword')
user.save()

My views.py

def registerpage(request):
   if request.method == 'POST':
      myusername = request.POST['name']
      email = request.POST['email']
      pass1 = request.POST['password1']
      pass2 = request.POST['password2']

      myuser = User.objects.get(username=myusername)
      User.set_password('pass1')

      myuser.name = myusername
      myuser.email = email
      myuser.password = pass1

      myuser.save()
      return redirect('testlogin')

The error that I am getting

TypeError at /register/
set_password() missing 1 required positional argument: 'raw_password'

I need help !

CodePudding user response:

Try myuser.set_password('pass1'). User is the class, you want to set the password of a specific user.

CodePudding user response:

create user

myuser = User.objects.create_user('username', '[email protected]', 'youPassword')

Update user data

user = User.objects.get(username=myusername)
user.set_password('newpassword')
user.save()

CodePudding user response:

In line 8 you are trying to get a user before creating that user, that's not possible, and in line no. 9, for which user you want to set password?(User is the class). first you have to create a user, after that, write get query.

def registerpage(request):
   if request.method == 'POST':
      myusername = request.POST['name']
      email = request.POST['email']
      pass1 = request.POST['password1']
      pass2 = request.POST['password2']
      if not pass1 == pass2:
          messages.warning(request,"please enter both password same")
          return render(request,'registerpagetemplate.html')
      else:   
          user = User.objects.create_user(username=myusername,password=pass1,email=email)
          return redirect('testlogin')
                 

After creating user, create a separate method for login, then execute query for access user:

myuser = User.objects.get(username=myusername)
  • Related