Django之URL的命名空间和命名模式
来源:互联网 发布:如何购买备案好的域名 编辑:程序博客网 时间:2024/06/05 03:42
Django之URL的命名空间和命名模式
http://blog.csdn.net/xiaobing_blog/article/details/11003643
django URL模式浅析
http://blog.csdn.net/laughing2333/article/details/51674905
Namespacing URL names¶
The tutorial project has just one app, polls
. In real Django projects, there might be five, ten, twenty apps or more. How does Django differentiate the URL names between them? For example, the polls
app has a detail
view, and so might an app on the same project that is for a blog. How does one make it so that Django knows which app view to create for a url when using the {% url %}
template tag?
The answer is to add namespaces to your URLconf. In the polls/urls.py
file, go ahead and add an app_name
to set the application namespace:
from django.conf.urls import urlfrom . import viewsapp_name = 'polls'urlpatterns = [ url(r'^$', views.index, name='index'), url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'), url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'), url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),]
Now change your polls/index.html
template from:
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
to point at the namespaced detail view:
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
xys友情提醒:
URL命名空间是用于模板文件中对URL(所谓URL是浏览器访问时的网络地址)的反向引用,一般会使用模板标签{% url %}
例如在index.html中
{% url 'polls:detail' question.id %}"这根模板文件的命名空间(其实质是本地的相对路径)不一样:模板文件的命名空间的应用场景是,在每个APP下建立appname/templates/appname/,然后在该目录下再放模板文件(index.html)。然后在需要引用模板文件的地方,采用模板文件相对地址硬编码时,形式如下:appname/index.html.
例如在views.py中
def index(request): #return HttpResponse("Hello, world. You're at the polls index.") return render(request,'polls/index.html')
或者index.html中
<li><a href="/polls/xxx.html">{{ question.question_text }}</a></li>
- Django之URL的命名空间和命名模式
- Django之URL的命名空间和命名模式
- Django url的命名空间 url反向解析
- 模式对象的命名规则和命名空间
- JavaScript之命名空间模式
- 嵌套的命名空间和匿名的命名空间
- JavaScript之命名空间模式 浅析
- 设计模式-命名空间
- 命名空间之全名命名空间
- XAML命名空间和命名空间映射
- 默认命名空间和根命名空间
- c++学习之命名空间和异常
- c++之命名空间和结构体
- 浅谈C#命名空间的命名规范和习惯
- XML之命名空间
- 读书笔记之 -->"命名空间"
- C++之命名空间
- C++之命名空间
- 业余草推荐一款局域网(内网)穿透工具lanproxy
- Altium Designer 导入Arduino UNO PCB
- JSONP 在前端的发送和后台node.js的处理
- HDU 1160 FatMouse's Speed
- 用量子计算辅助深度学习:研究者提出量子辅助Helmholtz机
- Django之URL的命名空间和命名模式
- android 布局绘制流程解析
- 汉字转拼音函数SQLSERVER数据库
- 人脸检测数据集评价代码FDDB evaluation运行方法
- extjs6-modern Ext.field.Select 弹出框的上方按钮 汉化
- 同时装了Python3和Python2,怎么运行.py及pip
- C#相关
- 吾爱破解crackme 018 019
- C# ListView定位并滚动到指定行