Django自定义登录表单HttpResponseRedirect无法正常工作

时间:2015-10-22 21:18:02

标签: django login

当我的登录成功时,我要么被重定向到/ login页面,要么HttpResponseRedirect无效。我希望用户在点击提交后通过成功的电子邮件和密码重定向到我的/ successful_login页面。我在哪里错过了什么?

views.py

from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect

from customauth.forms import RegistrationForm



def login_user(request):

    form = RegistrationForm()
    logout(request) #logs out user upon reaching the /login/ page

    email = password = ''
    if request.POST:
        form = RegistrationForm(request.POST)

        user = authenticate(email=email, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect('/successful_login/')
            else:
               state = "Your account is not active, please contact the administrator."
        else:
           state = "Your email and/or password were incorrect."

    state = "Please log in below..."

    context = RequestContext(request, {
        'state': state,
        'email': email,
        'form': form,
    })

    return render_to_response('customauth/login.html', {}, context)


@login_required(login_url='/login/')
def successful_login(request):
   return render_to_response('customauth/successful_login.html');

forms.py

from django import forms


class RegistrationForm(forms.Form):
    email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder':'Email'}))
    password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder':'Password'}))

的login.html

{% load widget_tweaks %}

<html>
<head>
    <title>Login</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

    {% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{% static 'customauth/style.css' %}">
</head>
<body>
<div class="container">    

    <div id="loginbox" class="mainbox col-md-6 col-md-offset-3 col-sm-6 col-sm-offset-3"> 

        <!--<div class="row">                
            <div class="iconmelon">
              <object type="image/svg+xml" data="customauth/static/customauth/barbell.svg">Your browser does not support SVG</object>
            </div>
        </div>-->

        <div class="panel panel-default" >
            <div class="panel-heading">
                <div class="panel-title text-center"><b>DATA STRONG</b></div>
            </div>     

            <div class="panel-body" >

            {% if form.errors %}
              <p>Invalid email or password! Please try again.</p>
            {% endif %}

                <form name="form" id="form" class="form-horizontal" method="POST">
                   {% csrf_token %}
                    <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                        {{ form.email|add_class:"form-control" }}
                    </div>

                    <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                        {{ form.password|add_class:"form-control" }}
                    </div>

                   <div class="input-group checkbox">
                      <label><input name="remember" type="checkbox">Remember me</label>
                   </div>

                    <div class="form-group">
                        <!-- Button -->
                        <div class="col-sm-12 controls">
                            <button type="submit" class="btn btn-primary pull-right" value="{{ next }}"><i class="glyphicon glyphicon-log-in"></i>Log in</button>                          
                        </div>
                    </div>

                </form>

            </div>    <!-----END OF BOOTSTAP CONTAINER----->                 
        </div>  
    </div>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

在进行身份验证之前,您需要从表单中提取邮件和密码,否则两个值都将为“”。 这可以通过以下方式完成:

email = form.cleaned_data['email']
password = form.cleaned_data['password']
user = authenticate(email=email, password=password)