django之创建第7-4个项目-配置views文件实现url传值
即:怎么实现url?name=xiaodeng&age=28等类似传值处理
1、配置views文件
# Create your views here. #coding:utf-8 from django.http import HttpResponse import datetime #导入templates文件所需导入库 from django.template import loader,Context class Person(): def __init__(self,name,age,sex): self.name=name self.age=age self.sex=sex def myMethod(self): return "get it" def time(request): t = loader.get_template("time.html") myid = request.GET.get("id") user = {"today": datetime.datetime.now(),"id":myid} c = Context(user) return HttpResponse(t.render(c)) def shengfen(request): t = loader.get_template("shengfen.html") user = {"shengfen":"湖北省恩施市"} c = Context(user) return HttpResponse(t.render(c)) def index(request): #加载器,加载模板 t=loader.get_template("index.html") # django之创建第4-1个项目-Dict形式 user = {"name": "xiaodeng", "sex": "male", "age": 28,"today":datetime.datetime.now()} c = Context({"test": user}) # 在这里test位变量,user为变量的值 return HttpResponse(t.render(c))
2、修改time.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>time</title> </head> <body> <li>1、当前时间:{{today}}</li><br> <li>id:{{id}}</li> </body> </html>
3、在浏览器中输入,http://127.0.0.1:8000/blog/time/?id=499,可得到效果
无语言基础,自学python所做的各种笔记,欢迎大牛指点.