Django 模板:创建一个“back"关联? [英] Django templates: create a "back" link?

查看:27
本文介绍了Django 模板:创建一个“back"关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Django,我想知道是否有一种简单的方法来创建一个使用模板系统返回"链接到上一页.

我认为在最坏的情况下,我可以从视图函数中的请求对象中获取此信息,并将其传递给模板渲染方法,但我希望我可以以某种方式避免所有这些样板代码.

我已经检查了 Django 模板文档,但没有看到任何明确提到这一点的内容.

解决方案

你可以启用:

'django.core.context_processors.request',

在您的 settings.TEMPLATE_CONTEXT_PROCESSORS 块中并勾出引荐来源网址,但这有点令人作呕,并且可能会到处崩溃.

大多数你想要这个的地方(例如 SO 上的编辑帖子页面)你有一个真实的对象可以连接到(在那个例子中,帖子)所以你可以轻松地计算出正确的前一页应该是什么.

I'm tooling around with Django and I'm wondering if there is a simple way to create a "back" link to the previous page using the template system.

I figure that in the worst case I can get this information from the request object in the view function, and pass it along to the template rendering method, but I'm hoping I can avoid all this boilerplate code somehow.

I've checked the Django template docs and I haven't seen anything that mentions this explicitly.

解决方案

Well you can enable:

'django.core.context_processors.request',

in your settings.TEMPLATE_CONTEXT_PROCESSORS block and hook out the referrer but that's a bit nauseating and could break all over the place.

Most places where you'd want this (eg the edit post page on SO) you have a real object to hook on to (in that example, the post) so you can easily work out what the proper previous page should be.

这篇关于Django 模板:创建一个“back"关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆