你好,我试图在数据库中创建auth_user用户,但它显示了一个错误
我正在使用的方法
from django.contrib.auth.models import User
user = Users.objects.get(username='myuser')
user.set_password('mypassword')
user.save()
我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')
我得到的错误
TypeError at /register/
set_password() missing 1 required positional argument: 'raw_password'
我需要帮助!
create user
myuser = User.objects.create_user('username', 'example@gmail.com', 'youPassword')
更新用户数据
user = User.objects.get(username=myusername)
user.set_password('newpassword')
user.save()
在第8行中,您试图在创建该用户之前获取该用户,这是不可能的,在第5行中。9、您要为哪个用户设置密码?(用户是类)。首先,你必须创建一个用户,然后,写get查询。
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')
创建用户后,为登录创建一个单独的方法,然后为访问用户执行查询:
myuser = User.objects.get(username=myusername)