繁体   English   中英

Django上下文处理器登录问题

[英]Django Context Processor Login In Issue

我有一个登录系统:

def login(request):
title = "Login"
if request.user.is_authenticated():
    return HttpResponseRedirect('/')
form = UserLoginForm(request.POST or None)
if request.POST and form.is_valid():
    username = form.cleaned_data.get('username')
    password = form.cleaned_data.get('password')
    user = auth.authenticate(username=username, password=password)
    if user:
        auth.login(request, user)
    return HttpResponseRedirect("/")# Redirect to a success page.
return render(request, 'accounts/login.html', {'title':title, 'form': form })


def logout(request):
    auth.logout(request)
    return HttpResponseRedirect('/accounts/login')

而且效果很好。 但是,当我尝试添加一个context_processor它停止工作并给出以下错误:

Environment:


Request Method: GET
Request URL: http://localhost:8000/accounts/login/

Traceback:

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/Users/andyxu/Documents/ece496-web/capstone/views.py" in login
  22.     return render(request, 'accounts/login.html', {'title':title, 'form': form })

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/shortcuts.py" in render
  30.     content = loader.render_to_string(template_name, context, request, using=using)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  68.     return template.render(context, request)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/backends/django.py" in render
  66.             return self.template.render(context)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py" in render
  206.                 with context.bind_template(self):

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py" in __enter__
  17.             return self.gen.next()

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/context.py" in bind_template
  236.             updates.update(processor(self.request))

Exception Type: TypeError at /accounts/login/
Exception Value: 'NoneType' object is not iterable

这是我的context_processor.py:

from matchalgorithm.models import Profile

def add_variable_to_context(request):
    if(request.user.id):
        profile = Profile.objects.filter(user_id = request.user.id).first()
    return {
        'main_profile': profile
    }

基本上,我只想检查用户是否有Profile否则返回None 我想使用此变量传递到我的base.html ,该视图不会由任何视图呈现。 有趣的是,一旦我logged in ,它就可以正常工作!

谢谢

您的上下文处理器上的缩进似乎已关闭,它与您的描述不符。 我假设return语句在if语句内,因为它与您的描述和回溯都匹配。

文档说(重点是我的):

上下文处理器具有一个非常简单的接口:这是一个Python函数,它接受一个参数,一个HttpRequest对象,并返回添加到模板上下文的字典。 每个上下文处理器必须返回一个字典。

因此,如果您不想在上下文中添加任何内容,则处理器必须返回一个空字典,而不是None

def add_variable_to_context(request):
    if(request.user.id):
        profile = Profile.objects.filter(user_id = request.user.id).first()
        return {
            'main_profile': profile
        }
    return {}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM