Skip to main content

No project description provided

Project description

Django Redux
=============================

Facilities to bridge the gap between Django and Redux.

Quickstart
----------

::

$ pip install django_redux
$ npm install django_redux

Create a file called `engine.py` for your project::

from django_redux.engine import action
from django_redux.consumers import ReduxConsumer


class MyConsumer(ReduxConsumer):

def connect(self, message, **kwargs):
if message.user.is_authenticated():
self.send({
'type': 'SET_USER',
'user': {
'username': self.message.user.username,
}
})

# This method will be called when the `INCREMENT_COUNTER` action gets
# fired from the JS via the WebsocketBridge (see below).
@action('INCREMENT_COUNTER')
def incr_counter(self, message):
self.group_send('broadcast', {'type': 'INCREMENTED_COUNTER', 'incrementBy': message['incrementBy']})

Create a file called `routing.py` for your project::

from channels.routing import route_class
from .consumers import MyConsumer

channel_routing = [
route_class(MyConsumer),
]

in your settings::

CHANNEL_LAYERS = {
'default': {
'BACKEND': 'asgi_redis.RedisChannelLayer',
'CONFIG': {
'hosts': [('localhost', 6379)],
},
'ROUTING': 'myproject.routing.channel_routing',
},
}

In your js entry point::

import React from 'react';

import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, } from 'redux';

import reducer from '../reducers';
import Root from '../containers/Root.react';

import { WebsocketBridge } from '../utils/WebsocketBridge';

const store = createStore(
reducer,
);


WebsocketBridge.connect();
WebsocketBridge.listen(store);

render(
<Provider store={store}>
<Root />
</Provider>,
document.getElementById('root')
);

To send an action from redux::

import { createAction } from 'redux-actions';

import ActionTypes from './constants';
import { WebsocketBridge } from 'django_redux';


export const incrementCounter = createAction(ActionTypes.INCREMENT_COUNTER, (incrementBy) => {
WebsocketBridge.send({
type: ActionTypes.INCREMENT_COUNTER,
incrementBy
});
});

To send an action from channels::

from django_redux import send_action

send_action('mygroup', {
'type': 'ACTION_NAME',
'payload': {'any': 'thing'},
})

Credits
-------

Most of this code is adapted from `johnpaulett/channel_chat <https://github.com/johnpaulett/channel_chat>`_.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_redux-0.0.2.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_redux-0.0.2-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django_redux-0.0.2.tar.gz.

File metadata

  • Download URL: django_redux-0.0.2.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for django_redux-0.0.2.tar.gz
Algorithm Hash digest
SHA256 8024eb7ab6f2f09ebd9cf0707b1f29198b8edc912e8af08d80da060337d6d62b
MD5 b952ebaeece099fa9ae6d94d2ebc2ddb
BLAKE2b-256 b31779692480c79a1454b66d2f0d024ac2cc01dfffd0732c06ba59ff5d707d3f

See more details on using hashes here.

File details

Details for the file django_redux-0.0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_redux-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bea1e681e6d502809227d205be0d42d25001448661f1c04cb636da9f9f1562a9
MD5 a89f2465381c127235365a5672d5c3f4
BLAKE2b-256 b893394c86cc52c26ddb8e79bbd73e5ab9cdb31a24754734d3afa850702f842a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page