flask重定向多条路线

flask重定向多条路线,第1张

flask重定向多条路线

更新:解决主要问题“我的路线出了什么问题”,最简单的调试方法是使用

app.url_map
;例如:

>>> app.url_mapMap([<Rule '/user/<id>/<username>/' (HEAD, OPTIONS, GET) -> profile>, <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>, <Rule '/user/<id>/' (HEAD, OPTIONS, GET) -> profile>])

在这种情况下,这确认端点设置正确。这是同时展示Plain

flask
和的示例
flask-classy

from app import app, modelsfrom flask import g, redirect, url_for, render_template, requestfrom flask.ext.classy import FlaskView, route@app.route('/user/<int:id>', strict_slashes=False)@app.route('/user/<int:id>/<username>', strict_slashes=False)def profile(id, username=None):    user = models.User.query.get_or_404(id)    if user.clean_username != username:        return redirect(url_for('profile', id=id, username=user.clean_username))    return render_template('profile.html', user=user)class ClassyUsersView(FlaskView):    @route('/<int:id>', strict_slashes=False)    @route('/<int:id>/<username>', strict_slashes=False, endpoint='classy_profile')    def profile(self, id, username=None):        user = models.User.query.get_or_404(id)        if user.clean_username != username: return redirect(url_for('classy_profile', id=id, username=user.clean_username))        return render_template('profile.html', user=user)ClassyUsersView.register(app)

它们具有不同的端点,你需要考虑以下因素url_for:

>>> app.url_mapMap([<Rule '/classyusers/<id>/<username>' (HEAD, OPTIONS, GET) -> classy_profile>, <Rule '/user/<id>/<username>' (HEAD, OPTIONS, GET) -> profile>, <Rule '/classyusers/<id>' (HEAD, OPTIONS, GET) -> ClassyUsersView:profile_1>, <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>, <Rule '/user/<id>' (HEAD, OPTIONS, GET) -> profile>])

没有

flask-classy
端点名称的是函数名称,但是如你所知,使用时这是不同的
classy
,你可以使用来查看端点名称,
url_map()
或使用来在端点名称中分配它
@route(..., endpoint='name')

减少重定向:
要在最小化重定向数量的情况下响应你发布的url,你需要使用

strict_slashes=False
,这将确保处理未以终止的请求,
/
而不是通过重定向将其
301
重定向到以其
/
终止的副本:

@app.route('/user/<int:id>', strict_slashes=False)@app.route('/user/<int:id>/<username>', strict_slashes=False)def profile(id, username=None):    user = models.User.query.get_or_404(id)    if user.clean_username != username:        return redirect(url_for('profile', id=id, username=user.clean_username))    return render_template('profile.html', user=user)

结果如下:

>>> client = app.test_client()>>> def check(url):...     r = client.get(url)...     return r.status, r.headers.get('location')... >>> check('/user/123')('302 FOUND', 'http://localhost/user/123/johndoe')>>> check('/user/123/')('302 FOUND', 'http://localhost/user/123/johndoe')>>> check('/user/123/foo')('302 FOUND', 'http://localhost/user/123/johndoe')>>> check('/user/123/johndoe')('200 OK', None)>>> check('/user/123/johndoe/')('200 OK', None)>>> check('/user/125698')('404 NOT FOUND', None)

行为strict_slashes:

with strict_slashes=FalseURL   Redirects/points to   # of redirects===========================================================================/user/123        302 /user/123/clean_username          1/user/123/       302 /user/123/clean_username          1/user/123/foo    302 /user/123/clean_username          1/user/123/foo/   302 /user/123/clean_username          1/user/123/clean_username    302 /user/123/clean_username          1/user/123/clean_username/   200 /user/123/clean_username/         0/user/125698     404with strict_slashes=True (the default)any non '/'-terminated urls redirect to their '/'-terminated counterpartURL   Redirects/points to   # of redirects===========================================================================/user/123        301 /user/123/  2/user/123/foo    301 /user/123/foo/         2/user/123/clean_username    301 /user/123/clean_username/         1/user/123/       302 /user/123/clean_username/         1/user/123/foo/   302 /user/123/clean_username/         1/user/123/clean_username/   200 /user/123/clean_username/         0/user/125698     404example:"/user/123/foo" not terminated with '/' -> redirects to "/user/123/foo/""/user/123/foo/" -> redirects to "/user/123/clean_username/"

我相信它完全可以满足你的测试矩阵的要求:)



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4931158.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存