简体   繁体   English

是否可以拥有 React 多页面应用程序,同时将 fastapi 作为后端路由每个页面并将数据作为 jinja 模板发送

[英]Is it possible to have React multi-page application while having fastapi as the backend routing each page and sending data as jinja templates

I'm working on a project where we are using vanilla javascript, Html and css for frontend while designing the backend in the FastAPI.我正在做一个项目,我们在 FastAPI 中设计后端时,我们在前端使用香草 javascript、Html 和 css。 The way backend connects with the frontend is using Templates, by adding the file name for a particular page and also sending the data as jinja template back to the client side.后端与前端连接的方式是使用模板,通过添加特定页面的文件名并将数据作为 jinja 模板发送回客户端。 We have five pages and all are declared in the backend as get requests and we are using a specific html file for loading a particular page.我们有五个页面,所有页面都在后端声明为获取请求,我们使用特定的 html 文件来加载特定页面。 So, there are mainly five html files for five pages Now, I was deciding to switch to reactjs as the frontend, however, react being the single page application and also just having the Routes for multiple pages, I'm wondering how to I connect it to the backend and also send data back to the client, the way I was doing it before.所以,五页主要有五个 html 文件现在,我决定切换到 reactjs 作为前端,但是,反应是单页应用程序,也只有多页的路由,我想知道如何连接它到后端并将数据发送回客户端,就像我以前做的那样。 I tried to look for answers, couldn't find one as of now... And also I'm kind of new to Reactjs我试图寻找答案,到目前为止找不到答案......而且我对 Reactjs 还是有点陌生

React, Python, and FastAPI are all great technologies that I too use. React、Python 和 FastAPI 都是我也使用的很棒的技术。 But they work more effectively when used for their intended purposes.但是,当用于预期目的时,它们会更有效地工作。 A hammer is a great tool if you need to pound in some nails, but it's a lousy tool for other purposes such as trying to use it as an eating utensil.如果您需要敲一些钉子,锤子是一个很好的工具,但对于其他用途(例如试图将其用作餐具)来说,它是一个糟糕的工具。 So you'd be well advised, if you are going to use React and FastAPI, to use them as they were intended.因此,如果您打算使用 React 和 FastAPI,建议您按照预期使用它们。

Jinja templates are used for server side rendering. Jinja 模板用于服务器端渲染。 But React is (in essence) also a server side rendering system.但 React(本质上)也是一个服务器端渲染系统。 Both of them will not play nicely together, at least not without more work and complexity.他们两个都不会很好地一起玩,至少不会没有更多的工作和复杂性。

Instead you'd be better off committing to either the Jinja templating or React.相反,您最好还是使用 Jinja 模板React。 If you choose Jinja, you figure out what the page will be (render) before sending it off to the client.如果您选择 Jinja,您会在将页面发送给客户端之前确定页面将是(渲染)什么。 If you choose React, you write the user-visible application, send that off to the client, and then the application on the client's browser fetches data through (usually JSON) APIs from the FastAPI back end.如果您选择 React,您将编写用户可见的应用程序,将其发送到客户端,然后客户端浏览器上的应用程序通过(通常是 JSON)API 从 FastAPI 后端获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM