未定义Django错误全局名称'csrf'

时间:2013-02-17 12:28:15

标签: django

我正在使用本教程制作论坛。 http://lightbird.net/dbe/forum1.html

我收到此错误,我不知道csrf来自哪些模块?我怎么知道?

 NameError at /forum/forum/1/

 global name 'csrf' is not defined

 Request Method:    GET
 Request URL:   http://127.0.0.1:8000/forum/forum/1/
 Django Version:    1.4.3
 Exception Type:    NameError
 Exception Value:   

 global name 'csrf' is not defined

 Exception Location:    C:\djcode\mysite\forum\views.py in add_csrf, line 25
 Python Executable:     C:\Python26\python.exe
 Python Version:    2.6.0
 Python Path:   

 ['C:\\djcode\\mysite',
  'C:\\Python26\\python26.zip',
  'C:\\Python26\\DLLs',
  'C:\\Python26\\lib',
  'C:\\Python26\\lib\\plat-win',
  'C:\\Python26\\lib\\lib-tk',
  'C:\\Python26',
  'C:\\Python26\\lib\\site-packages',
  'C:\\Python26\\lib\\site-packages\\PIL']

 Server time:   Sun, 17 Feb 2013 23:21:02 +1100

错误在这里。在我的views.py

  from django.core.urlresolvers import reverse
  from mysite.settings import MEDIA_ROOT, MEDIA_URL
  from forum.models import Forum
  from django.shortcuts import render_to_response
  from forum.models import Thread
  from django.core.paginator import Paginator, InvalidPage, EmptyPage
  def main(request):
      """Main listing."""
      forums = Forum.objects.all()
      return render_to_response("forum/list.html", dict(forums=forums, user=request.user))
  def forum(request, pk):
      """Listing of threads in a forum."""
      threads = Thread.objects.filter(forum=pk).order_by("-created")
      threads = mk_paginator(request, threads, 20)
      return render_to_response("forum/forum.html", add_csrf(request, threads=threads, pk=pk))
  def thread(request, pk):
      """Listing of posts in a thread."""
      posts = Post.objects.filter(thread=pk).order_by("created")
      posts = mk_paginator(request, posts, 15)
      title = Thread.objects.get(pk=pk).title
      return render_to_response("forum/thread.html", add_csrf(request, posts=posts, pk=pk,
    title=title, media_url=MEDIA_URL))
  def add_csrf(request, ** kwargs):
      d = dict(user=request.user, ** kwargs)
      d.update(csrf(request))
      return d

  def mk_paginator(request, items, num_items):
      """Create and return a paginator."""
      paginator = Paginator(items, num_items)
      try: page = int(request.GET.get("page", '1'))
      except ValueError: page = 1

      try:
          items = paginator.page(page)
      except (InvalidPage, EmptyPage):
          items = paginator.page(paginator.num_pages)
      return items

我不知道导入csrf的模块是什么

1 个答案:

答案 0 :(得分:2)

我从django.core.context_processors import csrf找到答案:]