基于 Django1.10 文档的深入学习(13)—— django.core.urlresolvers 之 reverse()
来源:互联网 发布:vb 数值转换为字符 编辑:程序博客网 时间:2024/06/15 11:29
reverse()
如果您需要在代码中使用类似于url模板标签的内容,Django将提供以下功能:
reverse(viewname,urlconf = None,args = None,kwargs = None,current_app = None)[source]
viewname
可以是URL
模式名称或可调用视图对象。例如,给出以下url
:
from news import viewsurl(r'^archive/$', views.archive, name='news-archive')
您可以使用以下任何一项来反转网址:
# using the named URLreverse('news-archive')# passing a callable object# (This is discouraged because you can't reverse namespaced views this way.)from news import viewsreverse(views.archive)
如果URL接受参数,您可以在args中传递它们。例如:
from django.urls import reversedef myview(request): return HttpResponseRedirect(reverse('arch-summary', args=[1945]))
args
和kwargs
不能同时传递给reverse()
。
如果不能匹配,则reverse()
会引发NoReverseMatch
异常。
reverse()
函数可以反转大量各种正则表达式模式的URL,但并不是每个可能的。目前的主要限制是该模式不能包含使用垂直条(“|”
)字符的替代选择。您可以非常高兴地使用这样的模式来匹配传入的URL并将其发送到视图,但是您不能反转这种模式。
current_app
参数允许您向解析器提供一个提示,指示当前执行的视图所属的应用程序。根据命名空间的URL
解析策略,此current_app
参数用作提示将应用程序名称空间解析为特定应用程序实例的URL
。
urlconf
参数是包含用于反转的URL
模式的URLconf
模块。默认情况下,使用当前线程的根URLconf
。
—-
what is reverse() in Django——StackOverFlow
in yoururls.py
define this:
url(r'^foo$', some_view, name='url_name'),
in a template you can then refer to this url as:
<!-- django <= 1.4 --><a href="{% url url_name %}">link which calls some_view</a><!-- django >= 1.5 or with {% load url from future %} in your template --><a href="{% url 'url_name' %}">link which calls some_view</a>this will be rendered as<a href="/foo/">link which calls some_view</a>
now say you want to do something similar in yourviews.py
- e.g. you are handling some other url (not/foo/
) in some other view (not some_view
) and you want to redirect the user to/foo/
(often the case on successful form submission)
you could just do
return HttpResponseRedirect('/foo/')
but what if you want to change the url in future - you’d have to update your urls.py
and all references to it in your code. This violates DRY (google it).
instead you can say
from django.core.urlresolvers import reversereturn HttpResponseRedirect(reverse('url_name'))
This looks through all urls defined in your project for the url defined with the name url_name
and returns the actual url /foo/
.
this means that you refer to the url only by itsname
attribute - if you want to change the url itself or the view it refers to you can do this by editing one place only - urls.py
. This whole idea of editing one place only is refered to as “Don’t Repeat Yourself” and is something to strive for.
- 基于 Django1.10 文档的深入学习(13)—— django.core.urlresolvers 之 reverse()
- 基于 Django1.10 文档的深入学习(11)—— django.shortcuts 之 render()
- 基于 Django1.10 文档的深入学习(12)—— django.shortcuts 之 redirect()
- 基于 Django1.10 文档的深入学习(10)—— django.contrib.auth 之 User model
- 基于 Django1.10 文档的深入学习(18)—— Using the Django authentication system 之 LoginRequiredMixin
- 基于 Django1.10 文档的深入学习(20)—— Using the Django authentication system 之 login()
- 基于 Django1.10 文档的深入学习(27)—— django.conf.urls utility functions 之 url(),include(),static()
- 基于 Django1.10 文档的深入学习(32)—— The Django admin site 之 基础
- 基于 Django1.10 文档的深入学习(33)—— Using the Django authentication system 之login_required和LogiRequiredMixin
- 基于 Django1.10 文档的深入学习(34)—— Customizing authentication in Django 之 基础
- 基于 Django1.10 文档的深入学习(15)——django.contrib.auth.hashers
- 基于 Django1.10 文档的深入学习(13)—— Making queries 之 Q objects
- 基于 Django1.10 文档的深入学习(2)—— Settings.py 之 STATIC_*
- 基于 Django1.10 文档的深入学习(3)—— models.py 之 FileField
- 基于 Django1.10 文档的深入学习(4)—— models.py 之 class Meta
- 基于 Django1.10 文档的深入学习(5)—— Making queries 之 FOO_set
- 基于 Django1.10 文档的深入学习(6)—— Translation 之 short_description
- 基于 Django1.10 文档的深入学习(8)—— Model field reference 之 choices
- shodan
- Java基础学习总结——equals方法
- js/jquery 杂七杂八
- getopt解析
- TensorFlow中cnn-cifar10样例代码详解
- 基于 Django1.10 文档的深入学习(13)—— django.core.urlresolvers 之 reverse()
- 阿里笔试题一
- VBA编码几个注意点
- 哈希表
- 【洛谷P3384】【模板】树链剖分
- 操作系统学习笔记(七)
- linux数据类型
- SQL truncate 、delete与drop区别
- 阿里笔试题二