GithubHelp home page GithubHelp logo

django-react-views's Introduction

Django-React-Views

This is a fork of django-react-views. There wasn't a public git repo of it and I might need to change it later, so I'm putting it here.


django-react-riews is a Django app providing generic Class Based views and template tags that make it easy to use react alongside django views and templates.

It can be used along with channels-redux-python and channels-redux-js to provide a framework to use websockets to keep a frontend redux state in sync with the database state.

Quick start

  1. Add "django_react_views" to your INSTALLED_APPS setting like this::

    INSTALLED_APPS = [
        ...
        'django_react_views',
    ]
  2. Create a directory in your app called react

  3. Copy webpack.config.js or use you're own webpack config as long as it builds your react components into one of your STATICFILES_DIRS

  4. Use npm to install at least the packages required for react & building with webpack

    npm install --save-prod react react-dom
    npm install --save-dev babel-core babel-loader babel-preset-env babel-preset-es2015 babel-preset-react glob webpack webpack-cli
  5. Add these scripts to package.json

    "scripts": {
        "build": "webpack",
        "watch": "webpack --watch"
    },
  6. Execute npm run watch to start building your react files

  7. Add the react template tag to the template where you want to show your react component

    {% load react %}
    ...
    {% react %}
    ...
    
  8. Create a view for your template

    from django_react_views.views import ReactDetailView
    class MyReactView(ReactDetailView):
        react_component = 'MyReactComponent.js'  # By default this will resolve to dist/app_name/{react_component}. If {% static %} can not find the file you may need to edit some other properties of this class
        model_serializer = MyModelSerializer
        model = MyModel
  9. Add a url for your view

    urlpatterns = [
        ...
        path('my-react-view/<int:pk>/', MyReactView.as_view(), name='my-react-view')
    ]
  10. This framework provides window.props, which contains a javascript object that can be used to hydrate your react state. This has the shape of::

    window.props = {"objects": {"appname.modelname": {"https://example.com/appname/modelname/1/": {object as serialized by your model serializer}} } }
  11. Start the development server and visit http://127.0.0.1:8000/ and visit your page to see you're react component in action

django-react-views's People

Stargazers

 avatar

Watchers

Dicky Mirrors avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.