Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No module named 'django.core.context_processors', in views.py

Tags:

python

django

In my django app, When I run server I am getting the error.

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

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

My views.py file is

   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')

and settings.py file is

"""
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/'
like image 888
Sonali Gupta Avatar asked Aug 03 '16 18:08

Sonali Gupta


2 Answers

Try to change this import to from django.template.context_processors import csrf. Your current import seems to be depreciated (via https://docs.djangoproject.com/en/1.9/releases/1.8/#django-core-context-processors)

like image 155
mateuszb Avatar answered Nov 14 '22 21:11

mateuszb


First, remove the invalid import that is causing the error

from django.core.context_processors import csrf

Next, the render_to_response shortcut is obsolete, you should use render instead. For example:

from django.shortcuts import render

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

The render shortcut uses a RequestContext automatic, so you don't have to manage the csrf token in the view manually.

like image 36
Alasdair Avatar answered Nov 14 '22 22:11

Alasdair