如何实现“后退” Django 模板上的链接?
我正在使用 Django 进行工具处理,我想知道是否有一种简单的方法来创建“后退”使用模板系统链接到上一页。
我认为在最坏的情况下,我可以从视图函数中的请求对象获取此信息,并将其传递给模板渲染方法,但我希望我能以某种方式避免所有这些样板代码。
我检查了 Django 模板文档,但没有看到任何明确提到这一点的内容。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
实际上它是
go(-1)
。Actually it's
go(-1)
.这个解决方案对我来说很有效:
但之前需要将
'django.core.context_processors.request',
添加到项目设置中的TEMPLATE_CONTEXT_PROCESSORS
中。This solution worked out for me:
But that's previously adding
'django.core.context_processors.request',
toTEMPLATE_CONTEXT_PROCESSORS
in your project's settings.这里
|escape
用于跳出" "
字符串。Here
|escape
is used to get out of the" "
string.好吧,您可以
在您的
settings.TEMPLATE_CONTEXT_PROCESSORS
块中启用:并挂钩引荐来源网址,但这有点令人恶心,并且可能会破坏整个地方。大多数你想要这个的地方(例如SO上的编辑帖子页面)你都有一个可以挂载的真实对象(在这个例子中,帖子),这样你就可以轻松地找出正确的上一页应该是什么。
Well you can enable:
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.
您始终可以使用非常简单的客户端选项:
You can always use the client side option which is very simple:
对于 RESTful 链接,“后退”通常意味着更高一级:
For RESTful links where "Back" usually means going one level higher:
这里提到的所有 Javascript 解决方案以及
request.META.HTTP_REFERER
解决方案有时都可以工作,但两者都会在同一场景(也许还有其他场景)中出现问题。我通常在创建或更改对象的表单下有一个取消按钮。 如果用户提交表单一次并且服务器端验证失败,则会再次向用户提供包含错误数据的表单。 你猜怎么着,
request.META.HTTP_REFERER
现在指向显示表单的 URL。 您可以按“取消”一千次,并且永远不会回到最初编辑/创建链接所在的位置。我能想到的唯一可靠的解决方案有点复杂,但对我有用。 如果有人知道更简单的解决方案,我很高兴听到它。 :-)
“技巧”是将初始的 HTTP_REFERER 传递到表单中并从那里使用它。 因此,当表单被发布时,它会传递正确的初始引用者。
我是这样做的:
我为完成大部分工作的表单创建了一个 mixin 类:
用于编辑/创建对象的表单(通常是 ModelForm 子类)可能如下所示:
视图必须传递当前请求到表格。 对于基于类的视图,您可以重写
get_form_kwargs()
:在显示表单的模板中:
All Javascript solutions mentioned here as well as the
request.META.HTTP_REFERER
solution sometimes work, but both break in the same scenario (and maybe others, too).I usually have a Cancel button under a form that creates or changes an object. If the user submits the form once and server side validation fails, the user is presented the form again, containing the wrong data. Guess what,
request.META.HTTP_REFERER
now points to the URL that displays the form. You can press Cancel a thousand times and will never get back to where the initial edit/create link was.The only solid solution I can think of is a bit involved, but works for me. If someone knows of a simpler solution, I'd be happy to hear from it. :-)
The 'trick' is to pass the initial HTTP_REFERER into the form and use it from there. So when the form gets POSTed to, it passes the correct, initial referer along.
Here is how I do it:
I created a mixin class for forms that does most of the work:
The form that is used to edit/create the object (usually a ModelForm subclass) might look like this:
The view must pass the current request to the form. For class based views, you can override
get_form_kwargs()
:In the template that displays the form:
对于 Django 管理员更改表单中的“后退”按钮,我最终要做的是自定义模板过滤器来解析和解码模板中的“preserved_filters”变量。 我将以下内容放在自定义 templates/admin/submit_line.html 文件中:
然后创建自定义模板过滤器:
For a 'back' button in change forms for Django admin what I end up doing is a custom template filter to parse and decode the 'preserved_filters' variable in the template. I placed the following on a customized templates/admin/submit_line.html file:
And then created a custom template filter:
使用客户端解决方案将是正确的解决方案。
Using client side solution would be the proper solution.
在 Web 开发中,有多种方法来实现后退按钮。 我想分享两种常见的方法。
此方法利用 JavaScript 的 History.back() 函数导航到浏览器历史记录中的上一页。 这是一个简单而有效的解决方案。
所有这些方法都会存储页面的历史记录,因此,如果您打开没有历史记录的页面并尝试返回,它将不起作用,只需重新加载页面即可。 这项工作按预期进行。
In web development, there are various methods to implement a back button. I'd like to share two common approaches.
This method utilizes JavaScript's history.back() function to navigate to the previous page in the browser history. It's a straightforward and effective solution.
All this ways make store an history of pages, so if you open the page without historic and try back it don't will work, just reload the page. This work as intend.