Django 개인 블로그 구축 자습서---자체 로그인 로그인 및 장식기 상세 설명

2349 단어 Django
하나, 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'),
]





2.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'

좋은 웹페이지 즐겨찾기