django-cms: apphooks不能使用reverse()或{% url %}

[英]django-cms: urls used by apphooks don't work with reverse() or {% url %}


I'm using django-cms with apphooks to display book detail information. I need the page with the app hook to accept a slug that specifies which book to display.

我正在使用django-cms和apphooks来显示图书细节信息。我需要带有应用钩子的页面来接受一个指定要显示哪本书的slug。

I created a page called 'books' and added the apphook 'BookDetailApp'.

我创建了一个名为“books”的页面,并添加了apphook“BookDetailApp”。

Here's what my books.cms_app file looks like:

这是我的书。cms_app文件看起来像:

class BooksApp (CMSApp):
    name = _('Book Detail Page Application')
    urls = ['books.urls']

apphook_pool.register(BooksApp)

Here's what my books.urls looks like:

这是我的书。url看起来像:

urlpatterns = patterns('',
    url(r'^(?P<slug>[\w\-]+)?', BookDetailView.as_view(), name='book_detail'),
)

And here's what my books.views file looks like:

这是我的书。视图文件看起来像:

class BookDetailView (DetailView):
    model = Book
    template_name = 'layouts/book-detail.html'
    context_object_name = 'book'

This all works fine when I go directly to book detail page. So going to http://localhost:8000/books/the-book-slug/ works exactly how I want to.

当我直接去book detail页面时,这一切都很好。因此,转到http://localhost:8000/books/the-book-slug/正是我想要的方式。

The problem is that I need be able to link to specific book detail pages from promos on the home page and none of the expected methods are working for me.

问题是,我需要能够从主页上的promos中链接到特定的图书细节页面,而所有的预期方法都不适合我。

Using the page_url template tag from django-cms doesn't work because it only accepts one argument, so i can't provide the slug needed to determine which book to display:

使用django-cms的page_url模板标签并不起作用,因为它只接受一个参数,所以我不能提供确定要显示哪本书的slug:

<a href="{% page_url 'book_detail' %}">go</a>

As expected this only redirects to http://localhost:8000/books/ which throws an error because the required slug was not included.

如预期的那样,这只会重定向到http://localhost:8000/books/,这会抛出一个错误,因为没有包含所需的slug。

So my next options are to use the url template tag or defining an get_absolute_url() function on the model. Neither of these options work:

因此,我的下一个选项是使用url模板标签或在模型上定义get_absolute_url()函数。这两种选择都不起作用:

<a href="{% url book_detail book.slug %}">go</a>

def get_absolute_url(self):
    return reverse('book_detail', args=[self.slug])

These both result in a NoReverseMatch: Reverse for 'book_detail' not found error.

这两个都导致了一个NoReverseMatch:反向的“book_detail”没有发现错误。

If I include the books.urls conf in my main url conf then it works. So it would appear that if the url is only being used by a cms apphook that it can't be reversed by django.

如果我把书包括在内。url conf在我的主url conf那么它就可以工作了。因此,如果这个url仅被一个cms apphook使用,那么django就无法逆转它。

Including books.urls in my main urls seems like a dirty solution and I definitely do not want to hardcode the urls in the template or the get_absolute_url function. Neither of those solutions seems very 'pythonesque'.

包括书籍。我的主url中的url看起来很脏,我肯定不想硬编码模板或get_absolute_url函数中的url。这两种解决方案都不像是“蟒蛇”。

Any suggestions?

有什么建议吗?

EDIT:

编辑:

Reverse works only if I use the language namespace. According to documentation specifying language namespace shouldn't be required.

只有当我使用语言名称空间时,反向才会工作。根据指定语言命名空间的文档,不应该被要求。

>>> reverse('en:book_detail', args=[book.slug])

1 个解决方案

#1


8  

This was apparently due to our application having cms.middleware.multilingual.MultilingualURLMiddleware which then forced all {% url %} template tags and the reverse() function to require the language namespace.

这显然是由于我们的应用程序有cms.middlewar .多语言的。multilingualurl中间件然后强制所有{% url %}模板标签和反向()函数来要求语言名称空间。

Since our site is not localized, removing the middleware worked fine. The documentation didn't seem that clear to me on this and finally found the answer from another source.

由于我们的站点没有本地化,删除中间件工作得很好。在这个问题上,文档对我来说似乎不是很清楚,最终从另一个来源找到了答案。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2012/06/26/75d256554a73a7113c64aacb07171926.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告