我正在尝试为我的示例项目创建一个登录和注销页面,并已将所有逻辑编码。我遵循的教程使用以下代码
from django.shortcuts import render
from basic_app.forms import UserForm
from django.contrib.auth import authenticate,login,logout
from django.http import HttpResponseRedirect,HttpResponse
from django.urls import reverse
from django.contrib.auth.decoraters import login_required
# Create your views here.
def index(request):
return render(request,'basic_app/index.html')
def register(request):
registered = False
if request.method == 'POST':
user_form = UserForm(data=request.POST)
if user_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
registered = True
else:
print(user_form.errors)
else:
user_form = UserForm()
return render(request,'basic_app/registration.html',
{'user_form':user_form,
'registered':registered })
@login_required
def special(request):
return HttpResponse("You are logged in!")
@login_required
def user_logout(request):
logout(request)
return HttpResponseRedirect(reverse('index'))
def user_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username,password=password)
if user:
if user.is_active:
login(request,user)
return HttpResponseRedirect(reverse('index'))
else:
return HttpResponse("Account Not Active")
else:
print("Someone tried to login and failed")
print("Username:{} and password: {}".format(username,password))
return HttpResponse("Invalid login details supplied")
else:
return render(request,'basic_app/login.html',{})
我得到的错误是这个
从django.contrib.auth.decoraters导入login_required
ModuleNotFoundError:没有名为“ django.contrib.auth.decoraters”的模块