为了账号安全,请及时绑定邮箱和手机立即绑定

如何在我自己的视图中使用 LoginView 并在 HTML 模板中显示?

如何在我自己的视图中使用 LoginView 并在 HTML 模板中显示?

jeck猫 2023-09-26 16:19:35
我想使用现成的 Django LoginView,但同时我必须使用我自己的视图在同一个 HTML 模板中注册。如果在 urls.py 文件中我将连接 2 个视图,那么我只会首先连接。所以我的问题是如何在我自己的视图中使用 LoginView 并使用 jinja 的 2 个表单?这是我的views.py 文件和html ;)def index(request):    if request.method == 'POST':        form = UserRegisterForm(request.POST)        formlog = auth_views.LoginView.as_view(template_name='main/index.html')        if 'signup' in request.POST:            if form.is_valid():                form.supervalid()                form.save()                username = form.cleaned_data.get('username')                messages.success(request, f'Dear {username} you have been created a new accound!')                return redirect('main')        elif 'login' in request.POST:            if formlog.is_valid():                formlog.save()                username = form.cleaned_data.get('username')                messages.success(request, f'Your account has been created! You are now able to log in')                return redirect('main')    else:        form = UserRegisterForm()        formlog = auth_views.LoginView.as_view(template_name='main/index.html')    return render(request, 'main/index.html', {'form': form, 'formlog': formlog})# this code is not working , but not returning any errors超文本标记语言{% if not user.is_authenticated %}    <!-- Login -->    <div class="container">        <div class="log-1">            <div class="log-0">                <p class="logtitle">BareTalk</p>                <form method="POST" name="login">                    {% csrf_token %}                    {{ formlog|crispy }}                    <button class="log-button first" type="submit">Login</button>                </form>                <button class="log-button second"                     onclick="modalwindowops.open();" id="signup">Sign Up</button>            </div>        </div>    </div>
查看完整描述

1 回答

?
慕丝7291255

TA贡献1859条经验 获得超6个赞

if 'signup' in request.POST:都不会elif 'login' in request.POST:在您的视图中触发index(),因为您的 HTML 表单实际上并不包含这些输入。请注意,该name元素已弃用该<form>属性。

相反,您可以在表单<input> 添加隐藏的内容,如下所示:

<form method="POST">

    {% csrf_token %}

    {{ formlog|crispy }}

    <input type="hidden" name="login" value="true" />

    <button class="log-button first" type="submit">Login</button>

</form>

还,


formlog = auth_views.LoginView.as_view(template_name='main/index.html')

将视图保存到formlog,而不是表单,因此调用formlog.is_valid()将导致错误。


代替


elif 'login' in request.POST:

    if formlog.is_valid():

        formlog.save()

        username = form.cleaned_data.get('username')

        messages.success(request, f'Your account has been created! You are now able to log in')

        return redirect('main')

你可能只需要做


elif 'login' in request.POST:

    log_view = auth_views.LoginView.as_view(template_name='main/index.html')

    log_view(request)

调用is_valid()、save()、 以及进行重定向都已完成LoginView。如果您仍想进行自定义,message.success()则必须重写一个或多个方法LoginView,但这是另一个主题。


更新:

您还需要将视图中的这一行:(formlog = auth_views.LoginView.as_view(template_name='main/index.html')之前return render...)更改为:


formlog = AuthenticationForm(request)

将此线引至街区外else。


还要在您的顶部添加表单的导入views.py:


from django.contrib.auth.forms import AuthenticationForm

需要进行此更改,因为模板需要表单对象(默认情况AuthenticationForm下LoginView)而不是视图对象。更新后的视图函数将如下所示:


from django.contrib.auth.forms import AuthenticationForm


def index(request):

    if request.method == 'POST':

        form = UserRegisterForm(request.POST)

        if 'signup' in request.POST:

            if form.is_valid():

                form.supervalid()

                form.save()

                username = form.cleaned_data.get('username')

                messages.success(request, f'Dear {username} you have been created a new accound!')

                return redirect('main')

        elif 'login' in request.POST:

            log_view = auth_views.LoginView.as_view(template_name='main/index.html')

            log_view(request)

    else:

        form = UserRegisterForm()

    formlog = AuthenticationForm(request)

    return render(request, 'main/index.html', {'form': form, 'formlog': formlog})

请注意,这可以通过在登录凭据无效时提供反馈来改进。事实上,如果提供的凭据不起作用,此更新的代码只会重新加载空白登录表单。


查看完整回答
反对 回复 2023-09-26
  • 1 回答
  • 0 关注
  • 45 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信