我有一个登陆页面,当用户导航到主页时会看到该页面。该页面同时具有登录表单和注册表单,登录按预期工作,但是当我尝试注册用户时没有任何反应。我没有收到任何错误或警告。
views.py
def login_page(request):
form = CreateUserForm()
context = {'form': form}
if request.user.is_authenticated:
return redirect('dashboard:dashboard')
elif request.method == 'POST':
if 'login-submit' in request.POST:
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('dashboard:dashboard')
else:
messages.info(request, 'Username OR password is incorrect')
elif 'register-submit' in request.POST:
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
form.save()
user = form.cleaned_data.get('username')
messages.success(request, 'Account was created for ' + user)
return redirect('dashboard:login')
return render(request, 'main/login.html', context)
登录.html
<form id="register-form" action="" method="POST" role="form" style="display: none;">
{% csrf_token %}
<div class="form-group">
{{form.username}}
</div>
<div class="form-group">
{{form.email}}
</div>
<div class="form-group">
{{form.password1}}
</div>
<div class="form-group">
{{form.password2}}
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 center">
<button type="submit" name="register-submit" id="register-submit" tabindex="4" class="form-control btn btn-register" value="Register">Register</button>
</div>
</div>
</div>
</form>
繁花不似锦
相关分类