def signup(request):
if request.user.is_authenticated:
return HttpResponseRedirect('/post/')
if request.method == 'GET':
form = UserCreationForm()
return render(request, 'tilweb/signup.html', {'form': form})
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
# https://docs.djangoproject.com/en/1.11/topics/forms/modelforms/#the-save-method
form.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=password)
login(request, user)
return HttpResponseRedirect('/post/')
else:
# If there were errors, we render the form with these
# errors
return render(request, 'tilweb/signup.html', {'form': form})
评论列表
文章目录