继续完善用户登录,基于类的
C:\Users\hlg\PycharmProjects\MxOnline\apps\users\views.py
# _*_ encoding:utf-8 _*_
from django.shortcuts import render
from django.contrib.auth import authenticate, login
from django.contrib.auth.backends import ModelBackend #
from django.db.models import Q # 完成并集
from django.views.generic.base import View #
# Create your views here.
from .models import UserProfile #
class CustomBackend(ModelBackend):
def authenticate(self, username=None, password=None, **kwargs):
# 完成自己的逻辑
try:
# user = UserProfile.objects.get(username=username)
user = UserProfile.objects.get(Q(username=username)|Q(email=username))
if user.check_password(password):
return user
except Exception as e:
return None
# 基于类
class LoginView(View): # django的View会自动调用get ,post
def get(self, request):
return render(request, "login.html", {})
def post(self, request):
user_name = request.POST.get("username", "")
pass_word = request.POST.get("password", "")
user = authenticate(username=user_name, password=pass_word)
if user is not None:
login(request, user) # 这个是系统提供的 login
return render(request, "index.html") # 登录成功,跳转到首页
else:
return render(request, "login.html", {"msg": "用户名或密码错误"}) # 登录失败,跳转到登录页面
# 这是基于函数的
# def user_login(request):
#
# if request.method == "POST":
# user_name = request.POST.get("username","")
# pass_word = request.POST.get("password","")
#
# user = authenticate(username=user_name, password=pass_word)
#
# if user is not None:
# login(request, user) # 这个是系统提供的 login
# return render(request, "index.html") # 登录成功,跳转到首页
# else:
# return render(request, "login.html", {"msg":"用户名或密码错误"}) # 登录失败,跳转到登录页面
# elif request.method == "GET":
# return render(request, "login.html", {})
C:\Users\hlg\PycharmProjects\MxOnline\MxOnline\urls.py
# _*_ encoding:utf-8 _*_
"""MxOnline URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Import the include() function: from django.conf.urls import url, include
3. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import url
from django.contrib import admin
from django.views.generic import TemplateView # 专门处理静态文件
import xadmin
# from users.views import user_login
from users.views import LoginView
urlpatterns = [
url(r'^xadmin/', xadmin.site.urls),
url('^$', TemplateView.as_view(template_name="index.html"), name="index"),
# url('^login/$', user_login, name="login")
url('^login/$', LoginView.as_view(), name="login") # as_view()方法是把类转换成方法使用
]
重启,退出系统,重新登录成功
下面介绍另外一个知识点:form
在users下面新建一个froms文件:
from django import forms
class LoginForm(forms.Form):
username = forms.CharField(required=True, min_length=3) # required=True 表示必填字段
password = forms.CharField(required=True)
C:\Users\hlg\PycharmProjects\MxOnline\apps\users\views.py