在模板中新建一个HTML页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="/login/" method="post">
用户名 <input type="text" name= "user">
密码 <input type="password" name="pwd">
<input type = 'submit'>
</form>
</body>
</html>
2.在Views中定义方面
from django.shortcuts import render,HttpResponse
# Create your views here.
def timer(request):
import time
ctime = time.time()
return render(request,'timer.html',{'ctime':ctime})
def special_case_2003(request):
return HttpResponse('special_case_2003')
def year_archive(request,year):
return HttpResponse('special_case_2003',year)
def month_archive(request,year,month):
return HttpResponse('special_case_2003', year,month)
def article_detail(request,year,month,day):
return HttpResponse('special_case_2003', year,month,day)
def login(request):
print(request.method)
if request.method=='GET':
return render(request,'login.html')
else:
print(request.GET)
print(request.POST)
return HttpResponse('ok')
增加路由分发
"""first_pro 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,re_path,include
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('timer/', views.timer),
path('login/',views.login),
#路由配置 路径--->s视图函数
#re_path(r'^articles/$',views.special_case_2003),
#路由分发
#re_path(r'app01',include('app01.urls')),
re_path(r'^', include('app01.urls')),
]
遇到一个问题,在login.html 中
<form action="/login/" method="post">
如果action 为空的话,应该是默认使用当前的,填写表单后,一段时间打印的print(request.method)
一直都是get请求,不知道为什么,然后我也不知道修改了什么,就得到了预期。
如果您也遇到这种情况,请告诉我,谢谢!~
网友评论