所以我是 django 的新手,对 python 的了解足以不称自己为初学者,但我绝不是专业人士。我只是想让用户身份验证在一个小型 django 应用程序上工作。我正在使用默认身份验证系统https://docs.djangoproject.com/en/1.8/topics/auth/default/并且内置表单、登录、注销等有自己的视图,但 UserCreationForm 没有有它自己的观点,所以我想我必须自己做。不知道我做错了什么。
这是我的意见.py
1 from django.shortcuts import render
2 from django.http import HttpResponse
3 from django.contrib.auth.forms import UserCreationForm
4 from django.contrib.auth import login
5
6 def home(request):
7 return HttpResponse("This is a barebones homepage")
8
9 def register(request):
10 registered = False
11
12 if request.method == 'POST':
13 user_form = UserCreationForm(data=request.POST)
14
15 if user_form.is_valid():
16 user = user_form.save()
17 username = user_form.get_username()
18 password = user_form.clean_password2()
19 login(request,user)
20 else:
21 print user_form.errors
22 else:
23 user_form = UserCreationForm()
24
25 return render(request, 'registration/register.html', {'user_form': user_form, 'registered': registered} )
~
这是我的 register.html
<!DOCTYPE html>
<html>
<head>
<title>Jet!</title>
</head>
<body>
{% if registered %}
Jet! says Thank you for registering!
<a href='/'>Return to the homepage.</a><br />
{% else %}
<form method="post" action="/register/">
{% csrf_token %}
{{ user_form.as_p }}
<input type="submit" name="submit" value="Register" />
</form>
{% endif %}
</body>
</html>