Django之URL的命名空间和命名模式
2017-09-12 11:24
441 查看
Django之URL的命名空间和命名模式
http://blog.csdn.net/xiaobing_blog/article/details/11003643
The tutorial project has just one app,
be five, ten, twenty apps or more. How does Django differentiate the URL names between them? For example, the
has a
How does one make it so that Django knows which app view to create for a url when using the
tag?
The answer is to add namespaces to your URLconf. In the
and add an
polls/urls.py
Now change your
polls/templates/polls/index.html
to point at the namespaced detail view:
polls/templates/polls/index.html
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>
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
pollsapp
has a
detailview, 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.pyfile, go ahead
and add an
app_nameto set the application namespace:
polls/urls.py
from django.conf.urls import url from . import views app_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.htmltemplate from:
polls/templates/polls/index.html
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
to point at the namespaced detail view:
polls/templates/polls/index.html
<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使用命名空间的错误
- 使用命名方式使用django的url模式
- 使用命名方式使用django的url模式
- Django url的命名空间 url反向解析
- django反向解析URL和URL命名空间的方法
- php设计模式 一 (命名空间 类自动载入 PSR标准)
- js的命名空间 && 单体模式 && 变量深拷贝和浅拷贝 && 页面弹窗设计
- JS进阶篇--命名空间模式解析
- PHP 设计模式 笔记与总结(1)命名空间 与 类的自动载入
- JS(十五)继承模式,命名空间,对象枚举(上)
- Javascript 命名空间模式
- php 设计模式_命名空间的巧用
- Django创建模板、URL模式、创建视图函数
- JavaScript 匿名函数、模块模式、闭包、命名空间、创建构造器(类)、继承
- HTTP 无法注册 URL http://+:8731/HelloWcfService/。进程不具有此命名空间的访问权限
- [0] WCF开发下,提示HTTP 无法注册 URL 进程不具有此命名空间的访问权限
- Django创建模板、URL模式、创建视图函数
- PHP中实现工厂类模式遭遇命名空间无法正确创建相应的解决代码