1.创建Django工程。
参考https://www.cnblogs.com/CK85/p/10159159.html中步骤。
2.在urls.py文件中添加url分发路径
"""Django_test URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/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
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.login, name='login'),
path('authentication/', views.authentication, name='authentication'),
]
路径分发时编辑name可以为路径取一个别名,在html文件中通过别名进行访问,避免过多的命名的改变。
3.在view.py中添加两个界面的试图函数。
from django.shortcuts import render, HttpResponse
import time
# Create your views here.
def login(request):
return render(request, 'login.html', locals())
def authentication(request):
# print(request.method)
user_name = request.GET.get('user')
pwd = request.GET.get('pwd')
user_dic = {
'ck': '123',
'ck1': '123',
'ck2': '123',
}
if user_name in user_dic:
if user_dic[user_name] == pwd:
print('authentication passed')
user = user_name
output = render(request, 'well_come.html', {'username': user})
else:
print('incorrect username or password')
output = HttpResponse('
incorrect username or password
')else:
print('不在')
output = HttpResponse('
the user dose not exist
')return output
login视图函数在用户访问时将login.html发送给用户。
authentication进行用户登入信息的验证和处理。
HttpResponse返回一个http响应。
4.在templates文件夹中创建对应的html文件:
login.html:
Title登入
手机号:
密码:
{% csrf_token %}
login界面是用户在登入时看到的界面,需要用户在该页面中输入账号和密码。
其中使用form表单进行读取数据,action=“/authentication/”与action=“{% url "authentiction" %}”相同,前者使用路径访问,后者使用别名访问。
在form表单中input标签type=’text‘ 和type=’submit‘组合使用,使得在用户点击提交按钮时使用form表单method中传入的方法将两个text中的值与text的name组成键值对传给在form表单action路径的文件。
well_come.html:
Title你好! {{ username }}
well_come界面为用户成功登入之后的欢迎界面。