【问题标题】:global name 'csrf' is not defined in Django 1.10全局名称“csrf”未在 Django 1.10 中定义
【发布时间】:2017-07-03 18:16:49
【问题描述】:
NameError at /todos/accounts/register/

global name 'csrf' is not defined

Request Method:     GET
Request URL:    http://localhost:8000/todos/accounts/register/
Django Version:     1.10.5
Exception Type:     NameError
Exception Value:    

global name 'csrf' is not defined

Exception Location:     /home/rahul/Desktop/apps/todolist/todos/views.py in register, line 37
Python Executable:  /usr/bin/python
Python Version:     2.7.6
Python Path:    

views.py 中的错误:

from django.shortcuts import render
from django.http import HttpResponse
from .models import Todo
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib.auth.forms import UserCreationForm

#from django.core.context_processors import csrf

def index(request):
    todos = Todo.objects.all()[:10]

    context = {
       'todos' : todos
    }
    return render(request, 'index.html', context)

def details(request, id):
    todo = Todo.objects.get(id=id)
    context = {
       'todo' : todo
    }
    return render(request, 'details.html', context)

 def register(request):
    if request.method == 'POST':
         form = UserCreationForm(request.POST)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/accounts/register/complete')

    else:
        form = UserCreationForm()
    token = {}
    token.update(csrf(request))
    token['form'] = form

    return render_to_response('registration/registration_form.html', token)

def registration_complete(request):
    return render_to_response('registration/registration_complete.html')

目前我的代码显示全局名称'csrf'未定义。为了克服这个错误,如果我取消注释 from django.core.context_processors import csrf,它会显示 context_processors module not found。请帮忙。

提前致谢。

【问题讨论】:

    标签: python-2.7 django-1.10


    【解决方案1】:

    Django 内置模板上下文处理器已从包 django.core 移至 django.template。因此,您应该将导入更改为

    from django.template.context_processors import csrf
    

    【讨论】:

      猜你喜欢
      • 2013-11-25
      • 1970-01-01
      • 2013-07-28
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 2015-12-22
      相关资源
      最近更新 更多