def home(request): #创建home函数处理请求
if request.method == "POST": #判断是否为post提交
#print(request.POST)
models.userinfo.objects.create( #提交表单的数据到数据库
name = request.POST['username'],
password = request.POST['password'],
age = request.POST['age'],
salary = request.POST['salary'],
)
data = models.userinfo.objects.all() #获取数据库数据
return render(request,'app01/home.html',{'data':data}) #渲染模版文件并传递数据库表给模版
#此处是以post方式提交,需要修改Django项目setting设置中的MIDDLEWARE,将csrf安全机制注销了:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
#'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
#建立路由:
#cat mysite/urls.py
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('home/',views.home),
]
重新启动uWSGI:
#uwsgi --stop uwsgi.pid
#uwsgi --ini uwsgi.ini
#浏览器访问:
#提交数据到数据库后并返回给浏览器客户端