How to get Django and ReactJS to work together?

前端 未结 9 1898
无人及你
无人及你 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条回答
  •  Happy的楠姐
    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']
        }
    };
    

提交回复
热议问题