How to get Django and ReactJS to work together?

前端 未结 9 1838
无人及你
无人及你 2020-12-04 04:12

New to Django and even newer to ReactJS. I have been looking into AngularJS and ReactJS, but decided on ReactJS. It seemed like it was edging out AngularJS as far as popular

相关标签:
9条回答
  • 2020-12-04 04:48

    The first approach is building separate Django and React apps. Django will be responsible for serving the API built using Django REST framework and React will consume these APIs using the Axios client or the browser's fetch API. You'll need to have two servers, both in development and production, one for Django(REST API) and the other for React (to serve static files).

    The second approach is different the frontend and backend apps will be coupled. Basically you'll use Django to both serve the React frontend and to expose the REST API. So you'll need to integrate React and Webpack with Django, these are the steps that you can follow to do that

    First generate your Django project then inside this project directory generate your React application using the React CLI

    For Django project install django-webpack-loader with pip:

    pip install django-webpack-loader
    

    Next add the app to installed apps and configure it in settings.py by adding the following object

    WEBPACK_LOADER = {
        'DEFAULT': {
                'BUNDLE_DIR_NAME': '',
                'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'),
            }
    }
    

    Then add a Django template that will be used to mount the React application and will be served by Django

    { % load render_bundle from webpack_loader % }
    
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width" />
        <title>Django + React </title>
      </head>
      <body>
        <div id="root">
         This is where React will be mounted
        </div>
        { % render_bundle 'main' % }
      </body>
    </html>
    

    Then add an URL in urls.py to serve this template

    from django.conf.urls import url
    from django.contrib import admin
    from django.views.generic import TemplateView
    
    urlpatterns = [
    
        url(r'^', TemplateView.as_view(template_name="main.html")),
    
    ]
    

    If you start both the Django and React servers at this point you'll get a Django error saying the webpack-stats.json doesn't exist. So next you need to make your React application able to generate the stats file.

    Go ahead and navigate inside your React app then install webpack-bundle-tracker

    npm install webpack-bundle-tracker --save
    

    Then eject your Webpack configuration and go to config/webpack.config.dev.js then add

    var BundleTracker  = require('webpack-bundle-tracker');
    //...
    
    module.exports = {
    
        plugins: [
              new BundleTracker({path: "../", filename: 'webpack-stats.json'}),
        ]
    }
    

    This add BundleTracker plugin to Webpack and instruct it to generate webpack-stats.json in the parent folder.

    Make sure also to do the same in config/webpack.config.prod.js for production.

    Now if you re-run your React server the webpack-stats.json will be generated and Django will be able to consume it to find information about the Webpack bundles generated by React dev server.

    There are some other things to. You can find more information from this tutorial.

    0 讨论(0)
  • 2020-12-04 04:52

    As others answered you, if you are creating a new project, you can separate frontend and backend and use any django rest plugin to create rest api for your frontend application. This is in the ideal world.

    If you have a project with the django templating already in place, then you must load your react dom render in the page you want to load the application. In my case I had already django-pipeline and I just added the browserify extension. (https://github.com/j0hnsmith/django-pipeline-browserify)

    As in the example, I loaded the app using django-pipeline:

    PIPELINE = {
        # ...
        'javascript':{
            'browserify': {
                'source_filenames' : (
                    'js/entry-point.browserify.js',
                ),
                'output_filename': 'js/entry-point.js',
            },
        }
    }
    

    Your "entry-point.browserify.js" can be an ES6 file that loads your react app in the template:

    import React from 'react';
    import ReactDOM from 'react-dom';
    import App from './components/app.js';
    import "babel-polyfill";
    
    import { Provider } from 'react-redux';
    import { createStore, applyMiddleware } from 'redux';
    import promise from 'redux-promise';
    import reducers from './reducers/index.js';
    
    const createStoreWithMiddleware = applyMiddleware(
      promise
    )(createStore);
    
    ReactDOM.render(
      <Provider store={createStoreWithMiddleware(reducers)}>
        <App/>
      </Provider>
      , document.getElementById('my-react-app')
    );
    

    In your django template, you can now load your app easily:

    {% load pipeline %}
    
    {% comment %} 
    `browserify` is a PIPELINE key setup in the settings for django 
     pipeline. See the example above
    {% endcomment %}
    
    {% javascript 'browserify' %}
    
    {% comment %} 
    the app will be loaded here thanks to the entry point you created 
    in PIPELINE settings. The key is the `entry-point.browserify.js` 
    responsable to inject with ReactDOM.render() you react app in the div 
    below
    {% endcomment %}
    <div id="my-react-app"></div>
    

    The advantage of using django-pipeline is that statics get processed during the collectstatic.

    0 讨论(0)
  • 2020-12-04 04:53

    I know this is a couple of years late, but I'm putting it out there for the next person on this journey.

    GraphQL has been helpful and way easier compared to DjangoRESTFramework. It is also more flexible in terms of the responses you get. You get what you ask for and don't have to filter through the response to get what you want.

    You can use Graphene Django on the server side and React+Apollo/Relay... You can look into it as that is not your question.

    0 讨论(0)
  • 2020-12-04 04:54

    A note for anyone who is coming from a backend or Django based role and trying to work with ReactJS: No one manages to setup ReactJS enviroment successfully in the first try :)

    There is a blog from Owais Lone which is available from http://owaislone.org/blog/webpack-plus-reactjs-and-django/ ; however syntax on Webpack configuration is way out of date.

    I suggest you follow the steps mentioned in the blog and replace the webpack configuration file with the content below. However if you're new to both Django and React, chew one at a time because of the learning curve you will probably get frustrated.

    var path = require('path');
    var webpack = require('webpack');
    var BundleTracker = require('webpack-bundle-tracker');
    
    module.exports = {
        context: __dirname,
        entry: './static/assets/js/index',
        output: {
            path: path.resolve('./static/assets/bundles/'),
            filename: '[name]-[hash].js'
        },
        plugins: [
            new BundleTracker({filename: './webpack-stats.json'})
        ],
    
     module: {
        loaders: [
          {
            test: /\.jsx?$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            query: {
              presets: ['es2015', 'react']
            }
          }
        ]
      },
    
    
      resolve: {
            modules: ['node_modules', 'bower_components'],
            extensions: ['.js', '.jsx']
        }
    };
    
    0 讨论(0)
  • 2020-12-04 04:58

    You can try the following tutorial, it may help you to move forward:

    Serving React and Django together

    0 讨论(0)
  • 2020-12-04 05:02

    I don't have experience with Django but the concepts from front-end to back-end and front-end framework to framework are the same.

    1. React will consume your Django REST API. Front-ends and back-ends aren't connected in any way. React will make HTTP requests to your REST API in order to fetch and set data.
    2. React, with the help of Webpack (module bundler) & Babel (transpiler), will bundle and transpile your Javascript into single or multiple files that will be placed in the entry HTML page. Learn Webpack, Babel, Javascript and React and Redux (a state container). I believe you won't use Django templating but instead allow React to render the front-end.
    3. As this page is rendered, React will consume the API to fetch data so React can render it. Your understanding of HTTP requests, Javascript (ES6), Promises, Middleware and React is essential here.

    Here are a few things I've found on the web that should help (based on a quick Google search):

    • Django and React API Youtube tutorial
    • Setting up Django with React (replaced broken link with archive.org link)
    • Search for other resources using the bolded terms above. Try "Django React Webpack" first.

    Hope this steers you in the right direction! Good luck! Hopefully others who specialize in Django can add to my response.

    0 讨论(0)
提交回复
热议问题