Django:如何返回上一个 URL [英] Django: How to return to previous URL

查看:133
本文介绍了Django:如何返回上一个 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是学习使用 Flask 使用 Python 开发 Web 应用程序的新手.现在我正在尝试通过使用 django 重做相同的应用程序来学习 django 1.9.现在我一直在尝试获取当前 URL 并将其作为参数传递,以便用户在下一页上的操作完成后可以返回.

Novice here who learned to develop a web app with python using Flask. Now I'm trying to learn django 1.9 by redoing the same app with django. Right now I am stuck at trying to get the current URL and pass it as an argument so that the user can come back once the action on the next page is completed.

在 Flask 中,要返回上一个 URL,我将使用 'next' 参数和 request.url 在更改页面之前获取当前 url.

In Flask, to return to a previous URL, I would use the 'next' parameter and the request.url to get the current url before changing page.

在模板中,您会发现如下内容:

In the template you would find something like this:

<a href="{{ url_for('.add_punchcard', id=user.id, next=request.url) }}">Buy punchcard :</a>

并在视图中:

redirect(request.args.get("next"))

我认为它与 django 大致相同,但我无法使其工作.我确实找到了一些建议,但它们适用于较旧的 django 版本(早于 1.5)并且不再起作用(随着解决方案的进行,它们非常震惊!)

I thought it would be about the same with django, but I cannot make it work. I did find some suggestions, but they are for older django version(older than 1.5) and do not work anymore(and they are pretty convulsed as solutions goes!)

现在,在我看来我正在使用

Right now, in my view I am using

return redirect(next)

注意:如果我根据网络上似乎总是使用 return HttpResponse(..., 所以我认为最近在如何做事情上发生了很多变化的解决方案判断,在 Django 中使用 return 重定向本身似乎是最近.

Note: The use of return redirect in django seems very recent itself if I judge by solutions on the web that always seem to use return HttpResponse(..., so I take it alot of changes happened lately in how to do things.

在模板中我有

<a href="{% url 'main:buy_punchcard' member.id next={{ request.path }} %}">Buy punchcard</p>

但这实际上返回了一个错误

but this actually return an error

无法解析余数:'{{' from '{{'

Could not parse the remainder: '{{' from '{{'

我确实在 settings.py 中添加了 context_processors

I did add the context_processors in settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
)

但这只是一连串错误中的最后一个错误.最重要的是,我无法让它工作.

But this is only the last error in a very long streak of errors. Bottom line is, I can't make it work.

因此,任何人都可以指出我在 Django 1.9 中如何做到这一点的正确方向?它看起来像一个非常基本的功能,所以我认为它会以某种方式更容易.

As such, anyone could point me in the right direction as to what is the way to do this in django 1.9? It look like a pretty basic function so I thought it would be easier somehow.

推荐答案

如果您希望 next 包含在查询字符串中,请将其移到 url 之外标签:

If you want next to be included in the query string, then move it outside of the url tag:

<a href="{% url 'main:buy_punchcard' member.id %}?next={{ request.path }}">Buy punchcard</p>

在您看来,您可以从 request.GET 获取 next,并使用 HttpResponseRedirect 返回重定向响应重定向快捷方式.

In your view, you can fetch next from request.GET, and return the redirect response using either HttpResponseRedirect or the redirect shortcut.

from django.utils.http import is_safe_url

next = request.GET.get('next', '/default/url/')
# check that next is safe
if not is_safe_url(next):
    next = '/default/url/'
return redirect(next)

请注意,重定向到从查询字符串中获取的 url 可能不安全.例如,它可以链接到不同的域.Django 有一个方法 is_safe_url 用于在登录或注销时检查下一个 url.

Note that it might not be safe to redirect to a url fetched from the query string. For example, it could link to a different domain. Django has a method is_safe_url that it uses to check next urls when logging in or out.

这篇关于Django:如何返回上一个 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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