python相關注冊登入方式
阿新 • • 發佈:2019-01-03
1、使用django自動功能實現登入/退出登入
1.1、登入
views.py:
from django.contrib.auth import authenticate,login,logout def acc_login(request): """登入頁面""" error_msg = '' if request.method == "POST": username = request.POST.get('username') password = request.POST.get('password') user= authenticate(username = username, password = password) if user: login(request,user) return redirect(request.GET.get('next','/index/')) # 如果有next則跳轉到next指定的頁面,如果沒有則跳轉到index頁面 else: error_msg = "Wrong username or password!" return render(request,"login.html",{'error_msg':error_msg})
1.2、退出登入
views.py:
def acc_logout(request): logout(request) return redirect("/login/")
urls.py:
from django.urls import path from NBCRM import views urlpatterns = [ path('login/',views.acc_login), path('logout/',views.acc_logout,name="logout"), ]
settings.py:
LOGIN_URL = '/login/'
登入介面:
未完待續。。