Django 平面页面(或多语言 ng)的可逆命名 URL?

发布于 2024-09-24 04:01:41 字数 160 浏览 9 评论 0原文

有没有办法为 Django 平面页面(或多语言-ng,平面页面 + 翻译)提供可逆的命名 URL?

如果没有,是否有类似的应用程序可以为可通过管理员编辑的页面命名 URL? (我宁愿避免任何庞然大物的 CMS。)

编辑:请参阅下面答案中的评论,以获取对问题的更多讨论和澄清。

Is there a way to have reversible, named URLs for Django flatpages (or multilingual-ng, which is flatpages + translations)?

If not, is there a similar app available which can have named URLs for pages which are editable through the admin? (And I'd rather avoid any behemoth CMSs, please.)

edit: Please see the comments in the answers below for more discussion and clarification of the question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌吟 2024-10-01 04:01:41

作为根 URLConf 中的最后一件事,输入

(r'^(?P<url>.*)

Now you can use the {% url %} 标记,其中包含视图名称 flatpage 和关键字参数 url 。您还可以在视图中使用 reverse() 函数。

, 'django.contrib.flatpages.views.flatpage'),

Now you can use the {% url %} 标记,其中包含视图名称 flatpage 和关键字参数 url。您还可以在视图中使用 reverse() 函数。

As the very last thing in your root URLConf, put

(r'^(?P<url>.*)

Now you can use the {% url %} tag, with the view name flatpage and the keyword argument url. You can also use the reverse() function in views.

, 'django.contrib.flatpages.views.flatpage'),

Now you can use the {% url %} tag, with the view name flatpage and the keyword argument url. You can also use the reverse() function in views.

若言繁花未落 2024-10-01 04:01:41

{% url %} 模板标签与 URLconf 配合使用,而平面页面则为 处理不存在的网址

如果您确实想将 Flatpages 反转为 URL,我的猜测是您必须编写自己的 {% flatpage_url %} 标记,如下所示:

    @register.simple_tag
    def flatpage_url(self, name):
        return FlatPage.objects.get({param}=name).url

-- 其中 {param} 是 < a href="http://code.djangoproject.com/browser/django/tags/releases/1.2.3/django/contrib/flatpages/models.py" rel="nofollow">FlatPage 模型字段。

此外,您可以将 {% url %}{% flatpageurl %} 标记合并在一起,以便后者回退到 reverse() 以防找不到 FlatPage (reverse() 就是 {% url %} 使用< /a>)。

编辑:

I don't accept any more upvotes as James Bennet's answer is The One for this question (and I'm ashamed I overlooked such trivial solution) - so please upvote it.

{% url %} template tag works with URLconf, whereas flatpages are handling nonexistent urls.

If you really want to reverse flatpages into URLs, my guess is that you have to write your own {% flatpage_url %} tag, which would looks like this:

    @register.simple_tag
    def flatpage_url(self, name):
        return FlatPage.objects.get({param}=name).url

-- where {param} is one of FlatPage model fields.

Also, you can merge {% url %} and {% flatpageurl %} tags together, so that the latter fallbacks to reverse() in case FlatPage is not found (and reverse() is what {% url %} uses).

EDIT:

I don't accept any more upvotes as James Bennet's answer is The One for this question (and I'm ashamed I overlooked such trivial solution) - so please upvote it.

来世叙缘 2024-10-01 04:01:41

所提出的解决方案不是与在模板中硬编码 URL 基本相同吗?在 href="URL" 中对 URL 进行硬编码与在 {% url URL %} 中进行硬编码有何区别?

我认为可以在以下位置找到更好的解决方案: 如何获取 Django Flatpages 模板的反向 URL,由 bufh 提出。

我认为实现这一目标的最佳方法是在 FlatPage 模型上添加一个额外的“名称”字段。

Aren't the proposed solutions basically the same as hardcoding the URL in the template? What's the difference between hardcoding the URL in href="URL" vs. doing it in {% url URL %}?

I think a better solution is found on: How can I get the reverse url for a Django Flatpages template, proposed by bufh.

I think the optimal way of achieving this would be adding an extra 'name' field on the FlatPage model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文