我使用的是 ModelForm,但没有使用 Django 的身份验证系统。我也在尝试设置一个随机密码,但已经注释掉了那个位,因为我不确定它是否会导致问题。
目前,以下将返回“成功”,但不会保存到数据库中。
def registration(request):
if request.POST:
data = request.POST.copy()
data['date_joined'] = datetime.date.today()
data['last_login'] = datetime.datetime.now()
form = UserForm(data)
if form.is_valid():
try:
User.objects.get(username=form.cleaned_data['username'])
except User.DoesNotExist:
#ran_pw = User.objects.make_random_password(length=12)
#user.set_password(ran_pw)
user = User()
user.username = form.cleaned_data['username']
user.first_name = form.cleaned_data['first_name']
user.m_init = form.cleaned_data['m_init']
user.last_name = form.cleaned_data['last_name']
user.institution = form.cleaned_data['institution']
user.department = form.cleaned_data['department']
user.phone = form.cleaned_data['phone']
user.email = form.cleaned_data['email']
user.save()
return HttpResponse('success')
else:
return HttpResponse(form.errors)
else:
form = UserForm()
return HttpResponse(form.non_field_errors)