Python Django学习
1、下载
python -m pip install Django==3.0.8 -i https://pypi.tuna.tsinghua.edu.cn/simple
2、新建一个项目
python django-admin.py startproject FirstDjango
django-admin.py目录
可切换到自己想要的目录创建,否则创建出的项目和django-admin.py同级
创建完后的目录:
urls.py中可以配置访问的url
settings.py中可以配置基础信息
3、启动服务
python manage.py runserver 0.0.0.0:8090
0.0.0.0表示其他机器通过IP可以访问你的部署(同一局域网下或者使用服务器发布,需要能ping通)
127.0.0.1表示仅本机可以访问
8090为端口号
打开网页输入http://127.0.0.1:8090/即可访问
4、添加默认登录页
(1)创建请求路径的py文件
在urls.py同级目录下创建一个views.py文件
views.py内容
from django.shortcuts import render
def login(request):
return render(request, 'login.html')
(2)创建静态资源文件夹
在manage.py同级目录下创建一个templates文件夹,在templates文件夹下创建css和js和img文件夹以及login.html文件
css和js以及img文件夹中放入bootstrap和jquery以及背景图片
login.html内容
(3)在urls.py中添加代码
(4)在settings.py中添加代码
(5)访问127.0.0.1:8090
5、前端接受后台数据
views.py添加代码
from django.shortcuts import render def login(request): return render(request, 'login.html') def get_data(request): data = {'data': '普通数据', 'data_list': ['列表数据1', '列表数据2', '列表数据3'], 'data_dict': {'name': '李华', 'age': 20}, 'data_list_dict': [{'name': '小白', 'age': 21}, {'name': '袁华', 'age': 20}], } return render(request, 'background.html', data)
urls.py添加代码
在login.html同级添加background.html
假设后台返回字典形式
data = {'data': '普通数据', 'data_list': ['列表数据1', '列表数据2', '列表数据3'], 'data_dict': {'name': '李华', 'age': 20}, 'data_list_dict': [{'name': '小白', 'age': 21}, {'name': '袁华', 'age': 20}], }
(1)正常取字典key
(2)字典中有列表
<p>这是列表的后台数据(所有):{{data_list}}</p> <p>这是列表的后台数据(取下标2):{{data_list.2}}</p>
(3)字典中有字典
<p>这是字典的后台数据(所有):{{data_dict}}</p> <p>这是字典的后台数据(取key为name):{{data_dict.name}}</p> <p>这是字典的后台数据(取key为age):{{data_dict.age}}</p>
(4)字典中有列表,列表中有字典
<p>两者结合:{{data_list_dict.0.name}}</p> <p>两者结合:{{data_list_dict.1.age}}</p>
(5)遍历列表
<div style="width: 300px;"> <table class="table table-bordered"> <caption>遍历列表</caption> <thead> </thead> <tbody> <tr> {% for i in data_list %} <td>{{i}}</td> {% endfor %} </tr> </tbody> </table> </div>
(6)遍历字典
<div style="width: 300px;"> <table class="table table-bordered"> <caption>遍历字典kyes</caption> <thead> </thead> <tbody> <tr> {% for i in data_dict.keys %} <td>{{ i }}</td> {% endfor %} </tr> </tbody> </table> </div> <div style="width: 300px;"> <table class="table table-bordered"> <caption>遍历字典values</caption> <thead> </thead> <tbody> <tr> {% for i in data_dict.values %} <td>{{ i }}</td> {% endfor %} </tr> </tbody> </table> </div> <div style="width: 300px;"> <table class="table table-bordered"> <caption>遍历字典items</caption> <thead> </thead> <tbody> <tr> {% for i,j in data_dict.items %} <td>{{ i }}----{{j}}</td> {% endfor %} </tr> </tbody> </table> </div> <div style="width: 300px;"> <table class="table table-bordered"> <caption>遍历字典+列表</caption> <thead> <tr> {% for i in data_list_dict.0.keys %} <td>{{i}}</td> {% endfor %} </tr> </thead> <tbody> {% for i in data_list_dict %} <tr>{% for j in i.values %} <td>{{ j }}</td> {% endfor %}</tr> {% endfor %} </tbody> </table> </div>
运行
6、后台接收前端数据
GET方式
(1)在login.html同级添加form_commit.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="/static/css/bootstrap.min.css"> <script src="/static/js/jquery-1.11.3.min.js"></script> <title>发送数据</title> </head> <body> <div style="width: 400px;"> <form class="form-horizontal" role="form" action="/form_commit" method="GET"> <div class="form-group"> <label for="name" class="col-sm-2 control-label">姓名</label> <div class="col-sm-10"> <input type="text" class="form-control" name='name' id="name" placeholder="请输入姓名"> </div> </div> <div class="form-group"> <label for="age" class="col-sm-2 control-label">年龄</label> <div class="col-sm-10"> <input type="text" class="form-control" name="age" id="age" placeholder="请输入年龄"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">发送</button> </div> </div> </form> </div> <p>后台数据接收的姓名:{{name}}</p> <p>后台数据接收的年龄+10岁:{{age}}</p> </body> </html>
(2)urls.py添加代码
(3)views.py添加代码
from django.shortcuts import render def login(request): return render(request, 'login.html') def get_data(request): data = {'data': '普通数据', 'data_list': ['列表数据1', '列表数据2', '列表数据3'], 'data_dict': {'name': '李华', 'age': 20}, 'data_list_dict': [{'name': '小白', 'age': 21}, {'name': '袁华', 'age': 20}], } return render(request, 'background.html', data) def form_commit_html(request): return render(request, 'form_commit.html') def form_commit(request): request.encoding = 'utf-8' data = dict() username = '未接受到前端数据' age = -10 print(request.GET) if 'name' in request.GET and request.GET['name']: username = request.GET['name'] print('name:{}'.format(username)) if 'age' in request.GET and request.GET['age']: age = request.GET['age'] print('age:{}'.format(age)) data['name'] = username data['age'] = int(age) + 10 return render(request, 'form_commit.html', data)
(4)运行
POST方式
(1)form_commit.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="/static/css/bootstrap.min.css"> <script src="/static/js/jquery-1.11.3.min.js"></script> <title>发送数据</title> </head> <body> <div style="width: 400px;"> <form class="form-horizontal" role="form" action="/form_commit" method="GET"> <div class="form-group"> <label for="name" class="col-sm-2 control-label">姓名</label> <div class="col-sm-10"> <input type="text" class="form-control" name='name' id="name" placeholder="请输入姓名"> </div> </div> <div class="form-group"> <label for="age" class="col-sm-2 control-label">年龄</label> <div class="col-sm-10"> <input type="text" class="form-control" name="age" id="age" placeholder="请输入年龄"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">发送</button> </div> </div> </form> </div> <p>后台数据接收的姓名:{{name}}</p> <p>后台数据接收的年龄+10岁:{{age}}</p> <h2>POST方式</h2> <div style="width: 400px;"> <form class="form-horizontal" role="form" action="/form_commit_post/" method="POST">
<div class="form-group"> <label for="name" class="col-sm-2 control-label">姓名</label> <div class="col-sm-10"> <input type="text" class="form-control" name='name' id="name" placeholder="请输入姓名"> </div> </div> <div class="form-group"> <label for="age" class="col-sm-2 control-label">年龄</label> <div class="col-sm-10"> <input type="text" class="form-control" name="age" id="age" placeholder="请输入年龄"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">发送</button> </div> </div> </form> </div> <p>后台数据接收的姓名:{{name_post}}</p> <p>后台数据接收的年龄+10岁:{{age_post}}</p> </body> </html>
一定要加上 {% csrf_token %}否则403错误
(2)urls.py
urlpatterns = [ path('admin/', admin.site.urls), path('', views.login), path('back_data/', views.get_data), path('form_commit.html/', views.form_commit_html), path('form_commit/', views.form_commit), path('form_commit_post/', views.form_commit_post), ]
(3)views.py
def form_commit_post(request): username = '未接受到前端数据-post' age = -10 if 'name' in request.POST and request.POST['name']: username = request.POST['name'] print('name_post:{}'.format(username)) if 'age' in request.POST and request.POST['age']: age = request.POST['age'] print('age_post:{}'.format(age)) data = dict() data['name_post'] = username data['age_post'] = int(age) + 10 return render(request, 'form_commit.html', data)
(4)运行