从request.post清除数据使用基于功能的视图登录

时间:2018-09-06 19:26:23

标签: django-views

我试图获取一个模式弹出窗口来使登录工作,但最终不得不将我的类窗体视图更改为html窗体,并基于函数视图运行该窗体,以便它可以在每个页面上运行。 现在,由于无法使用cleaned_data(),因此我将停留在如何清理从登录名中接收到的数据(电子邮件,密码)的问题上。

我希望使用form.valid(),但是使用html表单时,我不确定如何引用它。

视图:

def login2(request):
    if request.method == 'POST':
        email= str(request.POST['u'])
        password = str(request.POST['p'])
        user = authenticate(username=email, password=password)
        if user is not None:
            auth_login(request=request, user=user)
            return HttpResponseRedirect(reverse('home'))
        else:
            messages.error(request, "You have entered an invalid username or password")
        return redirect("home")

表格:

<div class="modal fade" id="login-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header" align="center">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                    </button>
                </div>

                <div id="div-forms">
                    <form id="login-form" action="{% url 'login2' %}" method="post" enctype="multipart/form-data">
                        {% csrf_token %}
                        <div class="modal-body">
                            <div id="div-login-msg" style="padding-bottom: 10px"><div id="icon-login-msg" class="glyphicon glyphicon-chevron-right"></div>
                                <span id="text-login-msg">Please enter your email and password.</span>
                            </div>
                            <div style="padding-bottom: 20px">
                            <input id="login_username" name = 'u' class="form-control" type="text" placeholder="Email" required>
                            </div>
                            <input id="login_password" name = 'p' class="form-control" type="password" placeholder="Password" required>

                        </div>
                        <div class="modal-footer">
                            <div class="container" align="center">
                                <button style="width: auto" type="submit" class="btn btn-primary btn-lg btn-block">Login</button>
                            </div>
                        </div>
                    </form>
                </div>    
            </div>
        </div>
</div>

感谢帮助。

0 个答案:

没有答案