Django urls.py重构及参数传递详解
1. 内部重构#
2. 外部重构#
website/blog/urls.py
website/website/urls.py
3. 两种参数处理方式 #
1. blog/index/?id=1234&name=bikmin#
#urls.py
url(r'^blog/index/$','get_id_name')
#views.py
from django.http import HttpResponse from django.template import loader,Context def get_id_name(request): html = loader.get_template("index.html") id = request.GET.get("id") name = request.GET.get("name") context = Context({"id":id,"name":name}) return HttpResponse(html.render(context))
#index.html
<body> <li>id:{{ id }}</li> <li>name:{{ name }}</li> </body>
效果如下
2. blog/index/1234/bikmin#
#urls.py
url(r'^blog/index/(\d{4})/(\w+)/$','blog.views.get_id_name')
#views.py
from django.http import HttpResponse from django.template import loader,Context def get_id_name(request,p1,p2): html = loader.get_template("index.html") context = Context({"id":p1,"name":p2}) return HttpResponse(html.render(context))
#index.html
<body> <li>id:{{ id }}</li> <li>name:{{ name }}</li> </body>
效果如下:
3.blog/index/1234/bikmin (和-2不一样的在于views.py,接收的参数名是限定的)#
#urls.py
#限定id,name参数名 url(r'blog/index/(?P<id>\d{4})/(?P<name>\w+)/$','get_id_name')
#views.py
from django.http import HttpResponse from django.template import loader,Context def get_id_name(request,id,name): html = loader.get_template("index.html") context = Context({"id":id,"name":name}) return HttpResponse(html.render(context))
#index.html
<body> <li>id:{{ id }}</li> <li>name:{{ name }}</li> </body>
效果如下
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
python获取异常信息exc_info和print_exc的使用
python通过sys.exc_info获取异常信息,通过traceback.print_exc打印堆栈信息,包括错误类型和错误位置等信息,本文就来介绍一下具体用法,感兴趣的可以了解一下2023-12-12Keras搭建分类网络平台VGG16 MobileNet ResNet50
这篇文章主要为大家介绍了Keras搭建分类网络平台VGG16 MobileNet ResNet50,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-05-05matplotlib之pyplot模块之标题(title()和suptitle())
这篇文章主要介绍了matplotlib之pyplot模块之标题(title()和suptitle()),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-02-02
最新评论