我只是在Django中创建了一个注册表单,由于某些原因我无法解密,因此该表单无效。我添加了一个字段,用户可以在其中插入注册令牌,以防万一。
表格:
class SignupForm(UserCreationForm):
token = forms.CharField()
class Meta:
model = User
fields = ('username', 'password')
模板:
<form action="/register/" method="post">
{% csrf_token %}
<label>Username:</label>{{ form.username }}<br>
<label>Password:</label>{{ form.password }}<br>
<label>Token:{{ form.token }}<br>
<button type="submit">Signup</button>
</form>
查看:
def signup(request):
if request.method == 'GET':
form = SignupForm()
return render(request, 'signup.html',{'form':form})
if request.method == 'POST' and request.user.is_authenticated == False:
form = SignupForm(request.POST)
if form.is_valid(): #this is where it seems to be stuck
user = form.save(commit=False)
username = form.cleaned_data('username')
password = form.cleaned_data('password')
token = form.cleaned_data('token')
if SignupToken.objects.filter(token = token).exists():
user.save()
db_token = RegistrationToken.objects.filter(token = token)
db_token.delete()
sign_in_user = authenticate(request, username, password)
if sign_in_user is not None:
login(request, sign_in_user)
return redirect('home')
else:
message = 'Something went wrong.'
return render(request, 'signup.html', {'message':message})