from django.contrib.auth import authenticate, login, logout
def log_in(request):
if request.method == 'GET':
form = LoginForm()
return render(request, 'login.html', {'form': form})
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['uid']
password = form.cleaned_data['pwd']
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
url = request.POST.get('source_url','/focus')
return redirect(url)
else:
return render(request,'login.html', {'form':form, 'error': "password or username is not ture!"})
else:
return render(request, 'login.html', {'form': form})
登录:
需要登录验证的操作函数:
在函数前使用 django的 @login_required 修饰就可以