[英]How to decompose URL into model and parameters in Django
Basically, I want the reverse version of get_absolute_url
method in Django model. 基本上,我想要Django模型中get_absolute_url
方法的反向版本。
For example, if we have a model: 例如,如果我们有一个模型:
class MyModel(Model):
...
get_absolute_url(self):
return reverse('myview', args=(myparam=self.myfield,))
then in some view we should be able to do something like this: 那么从某种角度来说,我们应该能够执行以下操作:
url = 'http://www.example.com/path/to/myview/123'
model, params = decompose(url)
# now model="MyModel", params=('123',)
EDIT: 编辑:
If it's too difficult to achieve, how about re-reversing views only? 如果太难实现了,那么只重新反转视图呢?
url = 'http://www.example.com/path/to/myview/123'
view, params = decompose(url)
# now view="MyView", params=('123',)
from django.core import urlresolvers
match = urlresolvers.resolve(urlsplit(url).path)
view = match.func
params = match.args
Solution from django-users . django-users的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.