我正在尝试在django中构建一个登录/注销应用程序。 这是我的view.py文件代码:
from django.core.contex_processors import csrf
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.contrib import auth
def login(request):
c = {}
c.update(csrf(request))
return render_to_response('login.html', c)
def auth_view():
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('/accounts/loggedin','')
else:
return HttpResponseRedirect('/accounts/invalid')
def loggedin(request):
return render_to_response('loggedin.html', {'full_name': request.user.username})
def invalid_login(request):
return render_to_response('invalid_login.html')
def logout(request):
auth.logout(request)
return render_to_response('logout.html')
这是我运行任何网址时抛出的错误:
Request URL: http://127.0.0.1:8000/accounts/login/
Django Version: 1.6.6
Exception Type: ImportError
Exception Value: No module named contex_processors
Exception Location: /home/mjrulesamrat/django_mj/django_test/django_test/views.py in <module>, line 1
这是我的settings.py文件,其中我添加了与csrf和身份验证以及Installed_app相关的中间件。
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'article',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
所以我在浏览器上显示了settings.py,views.py和错误。请帮忙。 哦,是的,我很想提到url.py文件.. 在这里。
from django.conf.urls import patterns, include, url
from article.views import HelloTemplate
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^articles/', include('article.urls')),
# Examples:
# url(r'^$', 'django_test.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^hello/$', 'article.views.hello'),
url(r'^hello_template/$', 'article.views.hello_template'),
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/login', 'django_test.views.login'),
url(r'^accounts/logout', 'django_test.views.logout'),
url(r'^accounts/auth', 'django_test.views.auth_view'),
url(r'^accounts/loggedin', 'django_test.views.loggedin'),
url(r'^accounts/invalid', 'django_test.views.invalid_login'),
)
这看起来很好..通常错误发生在views.py文件的第1行。
答案 0 :(得分:2)
你有一个拼写错误,它应该是:from django.core.context_processors import csrf
。