python - django-cms apphook url 不适用于使用 Python shell 的 reverse()

标签 python django shell django-cms

我已经创建了一个 django CMS apphook。不幸的是,我无法使用 Python shell 反转 apphook url。

cms_app.py 文件如下所示:

class ArticleApp (CMSApp):
    name = _('Article App')
    app_name = 'article_app'
    urls = ['article.urls']

apphook_pool.register(ArticleApp)

这是我的urls.py 文件:

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

模板文件是:

{% url 'article_app:article-by-slug' article.slug %}

模板内的 URL 反向执行符合预期。如果我尝试使用 Python shell 执行相同的操作,我会收到一条错误消息:

>>> from django.core.urlresolvers import reverse
>>> from article.models import Article
>>> a = Article.objects.get(pk=1)
>>> reverse('article_app:article-by-slug', kwargs={'slug': a.slug})
# Reverse for 'article_app:article-by-slug' with arguments '()' and keyword arguments '{'slug': 'this-is-article-1'}' not found.

在主 urls.py 中定义的其他 url 的工作方式与 shell 中预期的一样。只有 apphook 网址不起作用。

有什么建议吗?

谢谢!

最佳答案

感谢@Benjamin Wohlwend 我能够解决这个问题。 apphook 页面在“en-us”语言(Django 管理命令如“shell”的默认设置)中不可用。我必须在逆向之前激活正确的语言:

from django.utils import translation
translation.activate('de')

关于python - django-cms apphook url 不适用于使用 Python shell 的 reverse(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25342857/

相关文章:

shell - 避免 shell 脚本中的交互模式

python - 如何在 sqlite 查询中插入 Python 变量?

python - Gurobi 的多处理兼容性问题

python - Bootstrap 模板无法正常工作 Django

python - 自定义内联外键,不重复

python - 在Mac上使用pip安装Django时出错

python - 从用户 (URL) 获取参数

mysql - 在 shell 脚本中仅打印一次列名

python - 使用两个列表创建 python dict 时,如果键具有多个值时的键列表,我该如何制作它

python多重继承与父类有不同的__init__()