Django第二重点(thml与django连接)
导入url
1、进入app01,url
2、导入views模块
3、
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^text/', views.text),
url(r'^abc/',v2.text1),
]
4、进入views导入models
from app02 import models
def text1(request):
m=models.classes.objects.all()
return render(request,'text1.html',{'ust_list':m}
5、进入models进行定义类
class classes(models.Model):
title=models.CharField(max_length=23)
6、建立text.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="1"> {% for i in obj_list %} <tr> <td>{{ i.id }}</td> <td>{{ i.title }}</td> </tr> {% endfor %} </table> </body> </html>
7、数据分析:
def text(request):
#取出客户给的get数据(给什么拿什么)
id = request.GET.get('h')
print(id)
# m=models.student.objects.all()
#获取数据库数据(全部班级对象数据=m)
m= models.Classes.objects.all()
# print(type(m))
# #遍历m并且输出m里面全部集合数据()
# for i in m :
# print(i.id,i.title)
# 打开文件,读取内容,返回给用户
return render(request,'text.html',{'obj_list':m})
# f = open('templates/text.html','r')
# data = f.read()
# f.close()
# return HttpResponse(data)
# print(m)
# return HttpResponse("ok")
# render(request.GET.get('m'))
# render(request,models.student.objects.all())