我有一個登陸頁面,當用戶導航到主頁時會看到該頁面。該頁面同時具有登錄表單和注冊表單,登錄按預期工作,但是當我嘗試注冊用戶時沒有任何反應。我沒有收到任何錯誤或警告。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條經(jīng)驗 獲得超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
提交
取消