2013-02-17 125 views
1

我正在使用本教程進行論壇。 http://lightbird.net/dbe/forum1.htmlDjango錯誤全局名稱'csrf'未定義

我收到這個錯誤,我不知道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

回答

2

我找到了答案,從django.core.context_processors import csrf:]

相關問題