【问题标题】:Connect Django with react.js将 Django 与 react.js 连接起来
【发布时间】:2021-11-01 18:15:01
【问题描述】:

我正在开发 Django 项目。所以我用 HtmlBootstrap 构建了 UI。现在我想使用 reactjs,所以我需要知道如何在 react 中实现类似或现有的 UI,以及如何将 httprequest 和 response 从视图发送到模板。

function App() {
     return (
       <div className="App">
       <header className="App-header">
         <img src={logo} className="App-logo" alt="l ogo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
        <h1>wow sucess</h1>
      </header>
    </div>
    );
}

【问题讨论】:

    标签: javascript python reactjs django bootstrap-4


    【解决方案1】:

    React 现在是前端服务器,django 是后端。您需要使用fetch 函数或Axios 向django 服务器发送API 请求。

    【讨论】:

      【解决方案2】:

      如果您想从 Django 框架 API 中获取 API,那么您需要使用状态管理 Redux... 使用 Redux,您的应用程序的状态保存在存储中,每个组件都可以访问它需要的任何状态从这家商店更容易...您可以使用 axios 从 Django 框架中获取您的 API URL。

      查看此链接以了解更多信息:Axios

      以下是使用 Axios 获取 API 的示例:

      export const getItems = () => async (dispatch) => {
      
      
          try{
              const res = await axios.get(`YOUR API URL`)
              dispatch({
                  type: GET_ITEMS,
                  payload: res.data
              })
              
          }
      
      
          catch(e){
              dispatch({
                  type: ITEMS_ERROR,
                  payload: console.log(e),
              })
             
          }
      
      } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-10
        • 2014-12-05
        • 1970-01-01
        • 1970-01-01
        • 2017-09-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多