2017-04-02 7 views
1

我有一个登录系统:Django的背景处理器登录在发行

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,它工作正常!

感谢

回答

3

你的背景处理器压痕似乎是掉,它不符合你的描述。我假定return语句位于if语句内,因为它与您的描述和追踪相匹配。

docs说(重点煤矿):

A context processor has a very simple interface: It’s a Python function that takes one argument, an HttpRequest object, and returns a dictionary that gets added to the template context. Each context processor must return a dictionary.

所以,如果你不希望任何补充的背景下,您的处理器必须返回一个空的字典,而不是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 {} 
+0

啊哈,我完全错过了。谢谢! – anderish