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})
Operation functions that require login verification: Just use django’s @login_required modification before the function
Login:
Operation functions that require login verification:
Just use django’s @login_required modification before the function