python - views.py 中没有名为 'django.core.context_processors' 的模块

标签 python django

在我的 django 应用程序中,当我运行服务器时出现错误。

from . import views
from django.core.context_processors import csrf

ImportError: No module named 'django.core.context_processors'

我的views.py文件是

   from django.views.generic import View
from django.utils import timezone
from django.shortcuts import render, get_object_or_404,render_to_response,redirect
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.contrib.auth import authenticate,login
from django.core.context_processors import csrf
from .forms import UserForm

# Create your views here.

def home(request):
    return render(request, 'fosssite/home.html')

def login(request):
    c={}
    c.update(csrf(request))
    return render_to_response('fosssite/login.html',c)

def UserFormView(request):
    form_class=UserForm
    template_name='fosssite/signup.html'

    if request.method=='GET':
        form=form_class(None)
        return render(request,template_name,{'form':form})


        #validate by forms of django
    if request.method=='POST':
        form=form_class(request.POST)

        if form.is_valid():
            # not saving to database only creating object
            user=form.save(commit=False)
            #normalized data
            username=form.cleaned_data['username']
            password=form.cleaned_data['password']
            #not as plain data
            user.set_password(password)
            user.save() #saved to database

            user=auth.authenticate(username=username,password=password)
            if user is not None:
                auth.login(request,user)
                return HttpResponseRedirect('/')#url in brackets

            return render(request,template_name,{'form':form})

def auth_view(request):
    username=request.POST.get('username', '')
    password=request.POST.get('password', '')
    user=auth.authenticate(username=username,password=password)

    if user is not None:
        auth.login(request,user)
        return HttpResponseRedirect('/profileuser')#url in brackets
    else:
        return HttpResponseRedirect('/invalid')

def profileuser(request):
    return render_to_response('fosssite/profileuser.html',{'fullname':request.user.username})

def logout(request):
    auth.logout(request)
    pass

def edit_user_profile(request):
    pass

def invalid_login(request):
    return render_to_response('fosssite/invalid_login.html')

settings.py文件是

"""
Django settings for foss project.

Generated by 'django-admin startproject' using Django 1.9.7.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'fosssite',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'foss.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'foss.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
# read database.md file for configuring
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'

最佳答案

尝试将此导入更改为 from django.template.context_processors import csrf。您当前的进口商品似乎已折旧(通过 https://docs.djangoproject.com/en/1.9/releases/1.8/#django-core-context-processors )

关于python - views.py 中没有名为 'django.core.context_processors' 的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38751005/

相关文章:

python - 从脚本内更改目录后如何返回脚本目录?

python - 关于在 Python 中比较整数符号的函数实现的反馈

Javascript getElementbyId.value 不返回任何值

Django:从 Django View 进行测试时避免 HTTP API 调用

python - 在git中检查配置文件时 stash 密码

python - 用 Python 编写模式字母表程序

python - 如何从输入音频流中提取频率信息(使用 PortAudio)?

python - Numpy 随机选择,有概率生成具有唯一行的 2D 数组

python - Django:模型设计建议

python - Django 按天分组用 0 填充缺失数据