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

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

问题描述

这里的新手谁学会了使用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 redirect本身似乎是最近的事。我认为最近在做事上发生了很多变化。

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>

但这实际上返回错误


无法解析其余部分:'{{from 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',
)

但这只是a中的最后一个错误很长的错误条纹。底线是,我无法使其工作。

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>

在您看来,您可以提取 next request.GET 中返回,并使用 HttpResponseRedirect redirect 快捷方式。

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)

请注意,重定向到从查询字符串获取的网址。例如,它可以链接到其他域。 Django有一种方法 is_safe_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:如何返回上一个网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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