// views.py
从django.shortcuts导入渲染,重定向从.models导入insert_user
def index(request):
return render(request,'backpages/index.html')
def register(request):
if request.method=="POST":
first_name=first_name.objects.get(first_name=request.POST['first_name'])
email=email.objects.get(email=request.POST['email'])
password=password.objects.get(password=request.POST['password'])
user=insert_user(first_name=first_name,email=email,password=password)
user.save()
user=insert_user.objects.get(first_name=first_name,email=email,password=password)
print ("user created.")
return redirect('/')
else:
return render(request,'backpages/register.html')
models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, UserManager
class insert_user(AbstractBaseUser):
first_name=models.CharField(max_length=200)
email=models.CharField(max_length=200)
password=models.CharField(max_length=200)
如果您想注册用户,您的视图应该像这样
def register(request):
if request.method=="POST":
first_name=request.POST['first_name']
email=request.POST['email']
password=request.POST['password']
user= insert_user.objects.create(
first_name=first_name,
email=email)
user.set_password(password)
user.save()
print ("user created.")
return redirect('/')
return render(request,'backpages/register.html')
如果您输入密码 in insert_user.objects_create(
,它将创建纯文本密码。并且您将无法使用它登录,因为django希望使用settings.py中的SECRET_KEY哈希密码。
在为用户设置密码时,您应该使用.set_passwort('password123')
您认为此处出现问题: