1.http://127.0.0.1:8000/index/?username=zhangsan
如何获取url上username这个参数的值?
其实很简单
#coding:utf-8 from django.shortcuts import render,render_to_response # Create your views here. from django.http import HttpResponse def index(request): user = {'user_id':1,'username':'Apollo','age':28} getUserName = request.GET.get('username') print(getUserName) return render_to_response("index.html",user)
request.GET.get()就是获取url参数。
上面print getUserName打印结果为:Apollo
还可以这样写:
request.GET['username']
来学一下python的字典这个数据类型吧
user = {'user_id':1,'username':'Apollo','age':28}
这样就是定义字典
获取字典中某个key的值有2种方式:
print user.get('username') print user['username']
他们到底有什么区别呢?
我来常识打印一个没有的key,比如
print user.get('kk')
打印结果为:None
print user['kk']