我希望在成功登录后将我的用户重定向到最后一页。但是用户被重定向到索引页
这就是我在标题中的内容
<li><a href="/login">Login</a></li>
这是登录表单
<form method="post" action="{% url 'django.contrib.auth.views.login' %}" >
{% if form.errors %}
<p>Your username and password don't match our records. Please try again.</p>
{% endif %}
<div class="login">
<h1> LOG IN </h1>
<input type="text" class="form-control" id="username" placeholder="Email" name="username">
<input type="password" class="form-control" id="inputPassword" placeholder="Password" name="password">
<button class="btn btn-primary" type="submit" name="submit" id="ss-submit">LOG IN</button>
<input type="hidden" name="next" value="{{ next }}" />
<!-- <a href="/forgotpassword">Forgot your password?</a> -->
<p>Don't have an account? <a href="/signup">Sign up</a></p>
</div>
<!-- End Log In -->
{% csrf_token %}
</form>
这是登录
url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'meddy1/login.html'}, name="login"),
这是一个模板
def dentists(request):
d = getVariables(request)
doctors = Doctor.objects.all().order_by('-likes')
paginator = Paginator(doctors, 20) #Show 20 doctors per page
page = page = request.GET.get('page')
try:
doctors = paginator.page(page)
except PageNotAnInteger:
doctors = paginator.page(1)
except EmptyPage:
doctors = paginator.page(paginator.num_pages)
d['doctors'] = doctors
d['paginator'] = paginator
return render_to_response('meddy1/dentists.html',d)
答案 0 :(得分:0)
您无需查看登录信息;只是urls.py中的网址
from django.conf.urls import patterns
urlpatterns = patterns('',
(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'accounts/login.html'}),
您的模板可能是:
{% extends "base.html" %}
{% load i18n %}
{% block content %}
{% if form.errors %}
<p style="color:red;">username or passsword is wrong. Try again</p>
{% endif %}
<div class="login">
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<table class="login">
{{ form }}
</table>
<br><br><br>
Login
<input type="submit" value="Login"/>
<input type="hidden" name="next" value="{{ next }}" />
</form>
</div>
{% endblock %}
在您的视图中,如果它们是功能视图:
from django.http import HttpResponse, HttpResponseRedirect
def my_view(request):
if request.user.is_authenticated():
# do view stuff returning some HttpResponse
else:
return HttpResponseRedirect('/login/?next=%s' % request.path)
或者更简单,使用@login_required:
from django.contrib.auth.decorators import login_required
@login_required
def my_view(request):
# do view stuff returning some HttpResponse
如果您的观点是CBV,请将login_required放在urls.py中,如下所示:
from django.contrib.auth.decorators import login_required
url(r'^app/url/$', login_required(MyView.as_view()), name='my-view'),
在用户登录之前重定向到该页面。