gpt4 book ai didi

Flask, serving a React application: cannot refresh "pages"(为Reaction应用程序提供服务的FASK:无法刷新“页面”)

转载 作者:bug小助手 更新时间:2023-10-28 09:25:06 26 4
gpt4 key购买 nike



So I am serving a index.html file from Flask.
The index.html file comes from a built project using: https://github.com/facebookincubator/create-react-app

因此,我正在提供来自Flask.html的index.html文件。Html文件来自使用https://github.com/facebookincubator/create-react-app构建的项目



I have a couple routes setup in the React app, for example:
"/users"
and "/contracts"

我在Reaction应用程序中设置了几条路线,例如:“/USERS”和“/Contures”



When I refresh one of these routes, I get a 404 from flask, but while "clicking" on links found on the site, they work perfectly fine.

当我刷新这些路线中的一个时,我会从FASK得到一个404,但当我“点击”网站上找到的链接时,它们工作得非常好。


更多回答
优秀答案推荐

When you are clicking the links in the interface, React is re-rendering the page without any server-side intervention, and then updating the route in the URL bar. When loading however, you are making that route request to the server direct, and Flask does not have that route registered.

当您单击界面中的链接时,Reaction将在没有任何服务器端干预的情况下重新呈现页面,然后更新URL栏中的路线。但是,在加载时,您直接向服务器发出该路由请求,而Flask没有注册该路由。



Simplest way is to register these routes in the decorator for the function serving your homepage view

最简单的方法是在修饰器中为您的主页查看功能注册这些路径



@app.route('/')
@app.route('/users')
@app.route('/contracts')
def home_page():


If there are many many routes, you may want to use the catch-all URL pattern.

如果有许多许多路由,您可能想要使用捕获所有URL模式。



Specifying every route is too prone to error. Here is a more general solution.

指定每条路线太容易出错。这里有一个更一般的解决方案。


Add an error handler to catch 404:

添加错误处理程序以捕获404:


@app.errorhandler(404)
def handle_404(e):
if request.method == 'GET':
return redirect(f'/?request_path={quote_plus(request.path)}')
return e

The error handler will redirect to the home page, where your React works, passing the actual requested request_path as a parameter.

错误处理程序将重定向到您的反应工作的主页,将实际请求的REQUEST_PATH作为参数传递。


In your view handler do this:

在您的视图处理程序中执行以下操作:


@app.route('/')
def public_page_index(request_path=None):
return render_template('index.html',
request_path=request.args.get('request_path', ''))

The index.html template will create a hidden input:

Index.html模板将创建一个隐藏输入:


<input type="hidden" name="request_path" value="{{request_path}}">

Then the actual React path will be available for your React code to respond to. This is what I've done in my Home page component, using jquery and useHistory().

然后,您的反应代码将可以响应实际的反应路径。这就是我在我的主页组件中所做的,使用jQuery和useHistory()。


useEffect(() => {
// find if server refresh needs history
const $request_path = $('input[name=request_path]');
const val = $request_path.val();
if (val) {
$request_path.val("");
history.push(val);
return;
}
}, []);


I tried both previous answers , and at the end I found a general solution :

我尝试了前面的两个答案,最后我找到了一个通用的解决方案:


app = Flask(__name__, static_folder='../client/build', static_url_path='/')
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
@app.errorhandler(404)
def catch_all(path):
return app.send_static_file('index.html')

更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com