按照一些其他的SO答案 ,我使用的中间件来显示我的项目的每一个页面上的登录表单,这样用户可以就地登录。 我有些看不惯这种感知,但它确实使一个更好的用户体验。 理想情况下它会是异步的,但我还没有到达那里。
总之,middleware.py:
from MyProj.forms import MyProjTopLoginForm
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
#Notes: https://stackoverflow.com/questions/2734055/putting-a-django-login-form-on-every-page
class LoginFormMiddleware(object):
def process_request(self, request):
# if the top login form has been posted
if request.method == 'POST' and 'top_login_form-username' in request.POST:
# validate the form
form = MyProjTopLoginForm(prefix="top_login_form", data=request.POST)
if form.is_valid():
# log the user in
from django.contrib.auth import login
login(request, form.get_user())
# if this is the logout page, then redirect to /
# so we don't get logged out just after logging in
if reverse('logout') in request.get_full_path():
return HttpResponseRedirect('/')
#return HttpResponseRedirect('/')
else:
form = MyProjTopLoginForm(request, prefix="top_login_form")
# attach the form to the request so it can be accessed within the templates
request.top_login_form = form
class LogoutFormMiddleware(object):
def process_request(self, request):
if request.method == 'POST' and request.POST.has_key('logout-button') and request.POST['logout-button'] == 'logout':
from django.contrib.auth import logout
logout(request)
request.method = 'GET'
和views.py中:
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.db.models import Count
from django.core.urlresolvers import reverse
from django.views.generic import TemplateView
class Index(TemplateView):
template_name = 'KReport2/index.djhtml'
'''
def index(request):
return render(request, 'KReport2/index.djhtml')
'''
def logoutpage(request):
from django.contrib.auth import logout
logout(request)
return redirect(index)
你会注意到,我的评论旧索引视图功能,并与基于类的视图替换它。 通常我不会继承,而是在urls.py通过TEMPLATE_NAME,但在这里,这是除了点。 我的问题似乎是,中间件休息。 在UN /通行证填充索引页,然后提交,中间件捕捉形式,并记录我,但随后的Django接着返回空白HTTP响应。 没有错误,也没有渲染的索引页。 我刚刚拿到了不知道从哪里突破正在发生的事情。