Django 登录工作正常,但注册不工作

我有一个登陆页面,当用户导航到主页时会看到该页面。该页面同时具有登录表单和注册表单,登录按预期工作,但是当我尝试注册用户时没有任何反应。我没有收到任何错误或警告。


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>


杨__羊羊
浏览 90回答 1
1回答

繁花不似锦

您可以尝试以下操作并分享您的打印语句的日志吗?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if form.is_valid():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form.save()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user = form.cleaned_data.get('username')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messages.success(request, 'Account was created for ' + user)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect('dashboard:login')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print (form.is_valid())&nbsp; &nbsp;#form contains data and errors&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print (form.errors)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python