0
當我登錄成功時,我要麼重定向到/登錄頁面或HttpResponseRedirect不工作。我希望用戶點擊提交成功的電子郵件和密碼後重定向到我的/ successful_login頁面。我在哪裏錯過了什麼?Django自定義登錄表單HttpResponseRedirect不工作
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>