1, URL路径参数
定义路由URL时,使用正则表达式提取参数的方法从URL中获取请求参数,Django会将提取的参数直接传递到视图的传入参数中。
未命名参数按定义顺序传递
def weather1(request,city,year):
print(city)
print(year)
return HttpResponse('weather1')
url(r'^weather1/([a-z]+)/(\d{4})/$',views.weather1),
命名参数按名字传递
# http://127.0.0.1:8000/weather2/beijing/2018/
def weather2(request,city,year):
print(city)
print(year)
return HttpResponse('weather2')
url(r'^weather2/(?P<city>[a-z]+)/(?P<year>\d{4})/$', views.weather2),
2 Django中的QueryDict对象
HttpRequest对象的属性GET、POST都是QueryDict类型的对象
与python不同的是,QueryDict不是字典,他是用来处理同一个键具有多个值的
get():根据键获取值,键不存在则返回None值
getlist():根据键获取值,值以列表返回,可以获取指定键的所有值,键不存在则返回空列表[]
可以设置默认值.
3. 查询字符串Query String