带锚的烧瓶重定向URL [英] Flask redirect url with anchor

查看:99
本文介绍了带锚的烧瓶重定向URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从python代码重定向锚点时遇到一些问题.

我的代码:

func():
    ...
    redirect(url_for('my_view', param=param, _anchor='my_anchor'))

此重定向未将我重定向到#my_anchor.

在模板链接中,例如:

<a href="{{ url_for('my_view', param=param, _anchor='my_anchor') }}"></a>

效果很好...可能是烧瓶函数重定向"中的问题.

如何在Flask中使用重定向和锚点?

烧瓶版本0.10.x

解决方案

如果您的目标是重定向到在url中预先选择锚点的页面,我认为问题可能与您在"url_for".以下是我尝试执行您描述的操作.

views.py

from flask import Flask
from flask import redirect, url_for
app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


@app.route('/myredirect')
def my_redirect():
    return redirect(url_for('hello_world',_anchor='my_anchor'))


if __name__ == '__main__':
    app.run()

这不需要模板,点击/myredirect即可将您重定向到带有锚点#my_anchor的/p

使用$ python views.py开始查看后,导航至 http://127.0.0.1:5000/myredirect 最终出现在 http://127.0.0.1:5000/#my_anchor

I have some problem with redirecting with anchors from python code.

My code:

func():
    ...
    redirect(url_for('my_view', param=param, _anchor='my_anchor'))

This redirect didn't redirect me to #my_anchor.

In template link like:

<a href="{{ url_for('my_view', param=param, _anchor='my_anchor') }}"></a>

works good... May be problem in flask function "redirect".

How I can use redirect with anchors in Flask?

Flask version 0.10.x

解决方案

if your goal is to be redirected to a page with an anchor preselected in the url I think the problem may be connected to the function you have passed in the 'url_for'. Below is my attempt to do what you described.

views.py

from flask import Flask
from flask import redirect, url_for
app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


@app.route('/myredirect')
def my_redirect():
    return redirect(url_for('hello_world',_anchor='my_anchor'))


if __name__ == '__main__':
    app.run()

This does not need a template, as as soon as you hit /myredirect you are redirected to / with anchor #my_anchor

After you get your views started with $ python views.py and navigate to http://127.0.0.1:5000/myredirect you end up on http://127.0.0.1:5000/#my_anchor

这篇关于带锚的烧瓶重定向URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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