简体   繁体   English

使用额外选项反转 Django URL

[英]Reversing Django URLs With Extra Options

Suppose I have a URLconf like below, and 'foo' and 'bar' are valid values for page_slug .假设我有一个如下所示的 URLconf,并且'foo''bar'page_slug的有效值。

urlpatterns = patterns('',
    (r'^page/(?P<page_slug>.*)/', 'myapp.views.someview'),
)

Then, I could reconstruct the URLs using the below, right?然后,我可以使用下面的方法重建 URL,对吧?

>>> from django.core.urlresolvers import reverse
>>> reverse('myapp.views.someview', kwargs={'page_slug': 'foo'})
'/page/foo/'
>>> reverse('myapp.views.someview', kwargs={'page_slug': 'bar'})
'/page/bar/'

But what if I change my URLconf to this?但是如果我把我的 URLconf 改成这个呢?

urlpatterns = patterns('',
    (r'^foo-direct/', 'myapp.views.someview', {'page_slug': 'foo'}),
    (r'^my-bar-page/', 'myapp.views.someview', {'page_slug': 'bar'}),
)

I expected this result:我期待这个结果:

>>> from django.core.urlresolvers import reverse
>>> reverse('myapp.views.someview', kwargs={'page_slug': 'foo'})
'/foo-direct/'
>>> reverse('myapp.views.someview', kwargs={'page_slug': 'bar'})
'/my-bar-page/'

However, this throws a NoReverseMatch exception.但是,这会引发NoReverseMatch异常。 I suspect I'm trying to do something impossible.我怀疑我正在尝试做一些不可能的事情。 Any suggestions on a saner way to accomplish what I want?关于更明智的方式来完成我想要的任何建议?

Named URLs aren't an option, since I don't want other apps that link to these to need to know about the specifics of the URL structure (encapsulation and all that).命名 URL 不是一个选项,因为我不希望链接到这些的其他应用程序需要了解 URL 结构(封装和所有这些)的细节。

You should try naming your urlconfs.您应该尝试命名您的 urlconfs。 Example:例子:

urlpatterns = patterns('',
    url(r'^foo-direct/', 'myapp.views.someview', {'page_slug': 'foo'}, name='foo-direct'),
    url(r'^my-bar-page/', 'myapp.views.someview', {'page_slug': 'bar'}, name='bar-page'),
)

Then just edit your reverses and you should get it working.然后只需编辑你的反向,你应该让它工作。

>>> from django.core.urlresolvers import reverse
>>> reverse('foo-direct', kwargs={'page_slug': 'foo'})
'/foo-direct/'
>>> reverse('bar-page', kwargs={'page_slug': 'bar'})
'/my-bar-page/'

More info at: Django Docs更多信息请访问: Django 文档

Here's what we do.这就是我们所做的。

urls.py has patterns like this urls.py 有这样的模式

url(r'^(?P< datarealm >.*?)/json/someClass/(?P<object_id>.*?)/$', 'json_someClass_resource', ),

views.py as reverse calls like this views.py 像这样反向调用

    object = SomeModel.objects.get(...)
    url= reverse('json_someClass_resource', kwargs={'object_id':object.id,'datarealm':object.datarealm.name})

Named urls ought to be an option.命名网址应该是一个选项。 Your case is highlighted in the Django reference:您的案例在 Django 参考中突出显示:

http://docs.djangoproject.com/en/dev/topics/http/urls/?from=olddocs#id2 http://docs.djangoproject.com/en/dev/topics/http/urls/?from=olddocs#id2

I'm not sure the designers left another work-around;我不确定设计师是否留下了另一种解决方法。 they expected named urls to cover it.他们希望命名的网址能够覆盖它。

May I digress about encapsulation?我可以离题一下封装吗? Thanks.谢谢。 There are two main reasons:主要原因有两个:

  1. Abstraction--no one wants to see the details抽象——没有人愿意看到细节
  2. Security--no one should see the details安全性——任何人都不应看到细节

As for 1, you can get a decent amount of mileage out of it in python, and Django is an excellent example.至于1,你可以在python中获得相当多的里程,Django就是一个很好的例子。 As for 2, it's an interpreted language.至于2,它是一种解释语言。 Either you're running it where it's written, or you're shipping off compiled.pyc files.要么你在它写的地方运行它,要么你正在运送已编译的.pyc 文件。 If that's really what you're doing, then compile the url conf.如果这真的是你正在做的,那么编译 url conf。

Finally, it seems less encapsulated to let other apps know about the functions than the url structure.最后,与 url 结构相比,让其他应用了解功能似乎更少封装。 But if you really disagree, I think you'll have to implement a more flexible reverse method yourself.但如果你真的不同意,我认为你必须自己实现一个更灵活的反向方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM