我来自django背景,正在尝试获取flask中特定url的方法。
在django
from django.urls import resolve
resolve("/hello_world/") # returns view asssociated /hello_world/flask有没有类似的功能,可以针对url返回方法?
发布于 2021-06-21 13:08:28
您可能是在寻找redirect函数。
from flask import redirect
def your_function():
return redirect('/hello_world/')发布于 2021-06-21 13:15:50
您可以使用flask的redirect方法
from flask import redirect
@app.route('/hello_world/')
def home():
return redirect("/") # Redirects to /或
您可以结合使用flask的url_for函数和redirect函数来使用视图的方法名进行重定向。
from flask import redirect, url_for
@app.route('/')
def index():
return render_template('index.html') # Renders the template
@app.route('/hello_world/')
def hello():
return redirect(url_for('index')) # Redirects to /发布于 2021-06-21 14:25:41
from flask import redirect, url_for
@app.route('/')
def index():
contex_variable = 5 # as sample
return render_template('frontend.html', context = contex_variable)
# return render_template('frontend.html') 我们可以使用jinja在html模板中使用我们在上述代码中发送的上下文。你不需要使用上下文。
@app.route('/another_x/')
def another():
# write code here
return redirect(url_for('frontend')) # Redirects to /https://stackoverflow.com/questions/68062492
复制相似问题