Django Reverse的使用
来源:互联网 发布:部落冲突七本满防数据 编辑:程序博客网 时间:2024/05/17 17:44
reverse()
¶
If you need to use something similar to the url
template tag inyour code, Django provides the following function:
reverse
(viewname,urlconf=None, args=None, kwargs=None, current_app=None)[source]¶
viewname
can be aURL pattern name or thecallable view object. For example, given the followingurl
:
from news import viewsurl(r'^archive/$', views.archive, name='news-archive')
you can use any of the following to reverse the URL:
# 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)
If the URL accepts arguments, you may pass them in args
. For example:
from django.urls import reversedef myview(request): return HttpResponseRedirect(reverse('arch-summary', args=[1945]))
You can also pass kwargs
instead ofargs
. For example:
>>> reverse('admin:app_list', kwargs={'app_label': 'auth'})'/admin/auth/'
args
and kwargs
cannot be passed to reverse()
at the same time.
If no match can be made, reverse()
raises aNoReverseMatch
exception.
The reverse()
function can reverse a large variety of regular expressionpatterns for URLs, but not every possible one. The main restriction at themoment is that the pattern cannot contain alternative choices using thevertical bar ("|"
) character. You can quite happily use such patterns formatching against incoming URLs and sending them off to views, but you cannotreverse such patterns.
The current_app
argument allows you to provide a hint to the resolverindicating the application to which the currently executing view belongs.Thiscurrent_app
argument is used as a hint to resolve applicationnamespaces into URLs on specific application instances, according to thenamespaced URL resolution strategy.
The urlconf
argument is the URLconf module containing the URL patterns touse for reversing. By default, the root URLconf for the current thread is used.
更多关于Reverse的说明,参见django.urls
utility functions:https://docs.djangoproject.com/en/1.10/ref/urlresolvers/
- Django Reverse的使用
- Django reverse
- DJango — URL的Reverse和Resolve
- Django foreignKey reverse lookup
- Django中url的逆向解析 -> Reverse resolution of URLs
- Django中url的逆向解析 -> Reverse resolution of URLs
- Django之reverse在1.11与2.0的区别
- Django的使用
- django render_to_response的使用
- Django ModelForm的使用
- Django模板的使用
- Django框架的使用
- Django模型的使用
- Django模板的使用
- django.for的使用
- django框架的使用
- django主键的使用
- Django 的简单使用
- LWIP使用经验---变态级(好文章)
- 逆向工程
- oracle的游标
- c#回顾之泛型编程
- 【OpenCV】边缘检测
- Django Reverse的使用
- 闪亮亮
- [torch]maskzero-brnn
- 10 Wget Command Examples
- Java读取txt文件和写入txt文件
- hibernate.hbm2ddl.auto设置为updata时需要注意的地方
- Kafka 0.10.0 SASL/PLAIN身份认证及权限实现
- Android学习之Socket多个客户端聊天布局
- 西雅图的这家土豪公司,每天都在过双十一 ...