Django框架登录验证案例

 

 

 

from django.shortcuts import render

# Create your views here.

from django.shortcuts import HttpResponse

def login(request):
if request.method == "GET":
# 验证逻辑
user = request.POST.get("user")
pwd = request.POST.get("pwd")
if user == "huchangxi" and pwd == "123456":
return HttpResponse("用户名或密码验证失败")
else:
return HttpResponse("用户名或密码验证失败")
return render(request, "app02/login.html")
#return HttpResponse("hello login")

'''
def auth(request):
#验证逻辑
user = request.POST.get("user")
pwd = request.POST.get("pwd")
if user=="huchangxi" and pwd=="123456":
return HttpResponse("用户名或密码验证失败")
else:
return HttpResponse("用户名或密码验证失败")
'''

 

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

<form action="http://127.0.0.1:8000/login/" method="post">
用户名 <input type="test" name="user">
密码 <input type="password" name="pwd">
<input type="submit">
</form>

</body>
</html>

 

 

"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

#form app01 import views as v1
#from app02 import views as v2

from app01.views import get_timer
from app02.views import login
urlpatterns = [
path('admin/', admin.site.urls),
path("timer", get_timer),
path("index", get_timer),
path("login/", login),
]

 

 

 

posted @ 2022-06-28 20:28  呼长喜  阅读(35)  评论(0编辑  收藏  举报