0

和我一起

settings.py

urls.py

文件,我创建了一个

context_processors.py

同一目录下的文件。这是我的 context_processors.py 文件:

from django.contrib.auth.forms import AuthenticationForm

def loginFormCustom(request):
    form = AuthenticationForm()
    return {'loginForm': form,} 

这是我的views.py:

from django.template import RequestContext
from tp.context_processors import loginFormCustom

def main_page(request):

    variables = { 'title': 'This is the title of the page' }
    return render(request, 'main_page.html', variables, context_instance=RequestContext(request, processors = loginFormCustom))

现在,当我运行它并转到调用 main_page 视图的 URL 时,它在 / 处给了我一个 TypeError:

'function' object is not iterable

并且回溯导致:

return render(request, 'main_page.html', variables, context_instance=RequestContext(request, processors = loginFormCustom))

知道为什么吗?我是否正确使用上下文处理器?

4

1 回答 1

2

Django Docs 说 AuthenticationForm 就是这样,它是一个表单,所以我相信你需要以这种方式调用它:

from django.template import RequestContext
from django.contrib.auth.forms import AuthenticationForm

def main_page(request):
  if request.method == 'POST':
    form = AuthenticationForm(request.POST)
    # log in user, etc...
  else:
    form = AuthenticationForm()  # Unbound Form

  return render(request, 'main_page.html', context_instance=RequestContext(request, {'form': AuthenticationForm}))
于 2014-01-16T00:47:04.740 回答