我有一个登陆页面,当用户导航到主页时会看到该页面。该页面同时具有登录表单和注册表单,登录按预期工作,但是当我尝试注册用户时没有任何反应。我没有收到任何错误或警告。views.pydef 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>
1 回答
繁花不似锦
TA贡献1851条经验 获得超4个赞
您可以尝试以下操作并分享您的打印语句的日志吗?
if form.is_valid():
form.save()
user = form.cleaned_data.get('username')
messages.success(request, 'Account was created for ' + user)
return redirect('dashboard:login')
else:
print (form.is_valid()) #form contains data and errors
print (form.errors)
添加回答
举报
0/150
提交
取消