一、urls.py
from django.conf.urls import url,include
from paper import views
from rest_framework import routers
from django.views.decorators.csrf import csrf_exempt
app_name = 'paper'
urlpatterns = [
url('^$', views.PaperIndex.as_view(), name='index'),
url(r'^login/$', views.paper_login, name='login'),
url(r'^logout/$', views.paper_logout, name='logout'),
url(r'^forgot/$', views.paper_forgot, name='forgot'),
]
二、views.py
from django.shortcuts import render
from django.http.response import HttpResponse, HttpResponseBadRequest,HttpResponseRedirect
from django.contrib import auth
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import redirect
from django.utils.decorators import method_decorator
@method_decorator(login_required, name='dispatch')
class PaperIndex(View):
def get(self, request):
context = {
}
return render(request, 'paper_index.html', context=context)
@csrf_exempt
def paper_login(request):
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user:
login(request, user)
# return render(request, 'paper_index.html')
return redirect('/paper/')
else:
return render(request, 'login.html', {
'login_err': 'Please recheck your username or password !'
})
return render(request, 'login.html')
def paper_forgot(request):
return render(request, 'forgot.html')
def paper_logout(request):
logout(request)
return render(request, 'login.html')
def paper_register(request):
return render(request, 'register.html')
# Create your views here.
三、settings.py
LOGIN_URL = '/paper/login'
# 如果这里直接写login,则当装饰器重定向的时候会重定向到/accounts/login?next=
LOGOUT_URL = 'logout'