2018-08-14 13:16:37 387浏览
今天扣丁学堂Python培训老师给大家介绍一下关于Django框架详解,首先Django这个Web框架学习过的人无不知晓它的强大,Django是一个开放源代码的Web应用框架,由Python写成,采用了MT'V的框架模式.即Model,View,Template组成.许多成功的网站和APP都基于Django.说到底,其实Django内部就是对Socket连接的强大封装。打开PyCharm->File->NewProject进行创建
运行Django程序浏览器访问http://127.0.0.1:8000/表示连接成功了!
STATICFILES_DIRS = (os.path.join(BASE_DIR,'static'),)
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),)
# Author:TianTianBaby import socket def handle_request(client): buf = client.recv(1024) client.send("HTTP/1.1 200 OK\r\n\r\n".encode()) f = open('index.html', 'rb') data = f.read() client.send(data) def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost', 8000)) sock.listen(5) while True: connection, address = sock.accept() handle_request(connection) connection.close() if __name__ == '__main__': main()
from django.shortcuts import render,redirect def login(request): # request 包含用户提交的所有信息 error_msg = "" if request.method == 'POST': # 获取用户通过post 提交过来的数据 user = request.POST.get('user',None) pwd = request.POST.get('pwd',None) if user == 'admin' and pwd == "admin": #跳转到响应页面 ("/"相当于前面的地址127.1.0.0:8000) return redirect('/home') else: #用户名密码不匹配 error_msg = "错了 !!!!天啊!!" //error_msg 替换html文件 相同字段文字 return render(request, 'login.html',{'error_msg':error_msg})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>login</title> <style> label{ width: 80px; text-align: right; display: inline-block; } </style> </head> <body> <form action="/login/" method="post" style="background-color: cadetblue"> <p> <label for="username">用户名</label> <input id="username" name="user" type="text"/> </p> <p> <label for="password">密码</label> <input id="password" name="pwd" type="text"/> <input type="submit" value="提交"> //error_msg 替换请求 相同字段文字 <span style="color: red">{{ error_msg}}</span> </p> </form> </body> </html>
示例
USER_LIST = [] # # for index in range(20): # tem = {'username':'雪芙'+str(index),'sex':'女','age':20+index} # USER_LIST.append(tem) def home(request): if request.method == 'POST': #获取用户提交的数据 POST 请求中 u = request.POST.get('username') s = request.POST.get('sex') a = request.POST.get('age') tem = {'username': u, 'sex': s, 'age':a} USER_LIST.append(tem) //当首次没有数据时候,列表为空 return render(request,'home.html',{'user_list':USER_LIST})
home函数逻辑所对应的 templates文件中的home.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>home</title> </head> <body style="margin: 0"> <div style="height:48px;background-color: cadetblue"></div> <div> <form action="/home/" method="post"> <input type="text" name="username" placeholder="用户名"/> <input type="text" name="sex" placeholder="性别"/> <input type="text" name="age" placeholder="年龄"/> <input type="submit" value="添加"> </form> </div> <div> <table> //页面中的for循环写法 {% for row in user_list %} <tr> <td>{{ row.username }}</td> <td>{{ row.sex }}</td> <td>{{ row.age }}</td> </tr> {% endfor %} </table> </div> </body> </html>
示例
以上就是关于扣丁学堂Python开发培训Django框架详解的详细介绍,想要了解更多内容的小伙伴可以登录扣丁学堂官网咨询。扣丁学堂在线Python视频教程免费供学员观看学习,想要学好Python开发技术的小伙伴快快行动吧。扣丁学堂Python技术交流群:279521237。
【关注微信公众号获取更多学习资料】