Skip to content

Commit f779a43

Browse files
committed
add example of react routing
1 parent e87c6c1 commit f779a43

7 files changed

Lines changed: 75 additions & 50 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ client application build and Django's opinionated pipeline/staticfiles systems.
66

77
In short, we'll let Django do what it is good at; Server-side/ORM stuff, while de-coupling the client for greater flexibility (and less confusing black-box Django magic!)
88

9+
## routing notes
10+
* Routing (e.g. `/`, `/about`) has been configured using `react-router-dom`.
11+
* In our Django apps `urls.py` we have defined a catchall which essentially defers routing to React.
12+
* There is also an example url configured to allow django to serve a route template (depending on your use case you may want a mix of both approaches).
13+
914
## todo
10-
* Routing, React?
1115
* Client/Server request auth.
1216

1317
## install, build, run etc.
@@ -16,7 +20,7 @@ From project root:
1620
```shell
1721
# javascript things
1822
npm install # install js packages
19-
npm run watch # run webpack build for client app, and rebuild on change*
23+
npm run watch # run webpack build for client app, and rebuild on \*change
2024

2125
# python things
2226
virtualenv venv # create python virtualenv

‎assets/client/js/index.jsx‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { Component } from 'react';
2+
var ReactDOM = require('react-dom');
3+
4+
// Import routing components
5+
import { BrowserRouter, Route } from 'react-router-dom';
6+
7+
class Home extends Component {
8+
render(){
9+
return (<h1>Home Page</h1>);
10+
}
11+
}
12+
13+
class About extends Component {
14+
render(){
15+
return (<h1>About page</h1>);
16+
}
17+
}
18+
19+
/*Each route is defined with Route*/
20+
ReactDOM.render(
21+
<BrowserRouter>
22+
<div>
23+
<Route exact path="/" component={Home}/>
24+
<Route path="/about" component={About}/>
25+
</div>
26+
</BrowserRouter>,
27+
document.getElementById('react-app')
28+
);

‎assets/js/app.jsx‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎assets/js/index.js‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎myapp/urls.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
from . import views
33

44
urlpatterns = [
5-
url(r'^$', views.index, name='index'),
5+
6+
# match all pages to our index so React can handle routing
7+
url(r'^(?:.*)/?$', views.index),
8+
9+
# how you might explicitly define route
10+
# url(r'^$', views.index, name='index'),
11+
12+
# example of how to static template from django just for fun
613
url(r'^test/', views.test, name='test'),
714
]

‎package.json‎

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
{
2-
"name": "react-webpack-django",
3-
"version": "0.0.1",
4-
"description": "",
5-
"main": "index.js",
6-
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1",
8-
"build": "./node_modules/.bin/webpack --config webpack.config.js",
9-
"watch": "./node_modules/.bin/webpack --config webpack.config.js --watch"
10-
},
11-
"repository": {
12-
"type": "git",
13-
"url": "git+https://github.com/danwild/react-webpack-django.git"
14-
},
15-
"author": "",
16-
"license": "MIT",
17-
"bugs": {
18-
"url": "https://github.com/danwild/react-webpack-django/issues"
19-
},
20-
"homepage": "https://github.com/danwild/react-webpack-django#readme",
21-
"devDependencies": {
22-
"babel": "^6.23.0",
23-
"babel-core": "^6.25.0",
24-
"babel-loader": "^7.1.1",
25-
"babel-preset-env": "^1.6.0",
26-
"babel-preset-es2015": "^6.24.1",
27-
"babel-preset-react": "^6.24.1",
28-
"react": "^15.6.1",
29-
"react-dom": "^15.6.1",
30-
"webpack": "^3.5.4",
31-
"webpack-bundle-tracker": "^0.2.0"
32-
}
2+
"name": "react-webpack-django",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"build": "./node_modules/.bin/webpack --config webpack.config.js",
9+
"watch": "./node_modules/.bin/webpack --config webpack.config.js --watch"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/danwild/react-webpack-django.git"
14+
},
15+
"author": "",
16+
"license": "MIT",
17+
"bugs": {
18+
"url": "https://github.com/danwild/react-webpack-django/issues"
19+
},
20+
"homepage": "https://github.com/danwild/react-webpack-django#readme",
21+
"devDependencies": {
22+
"babel": "^6.23.0",
23+
"babel-core": "^6.25.0",
24+
"babel-loader": "^7.1.1",
25+
"babel-preset-env": "^1.6.0",
26+
"babel-preset-es2015": "^6.24.1",
27+
"babel-preset-react": "^6.24.1",
28+
"react": "^15.6.1",
29+
"react-dom": "^15.6.1",
30+
"react-router-dom": "^4.1.2",
31+
"webpack": "^3.5.4",
32+
"webpack-bundle-tracker": "^0.2.0"
33+
}
3334
}

‎webpack.config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var BundleTracker = require('webpack-bundle-tracker');
55
module.exports = {
66
context: __dirname,
77

8-
entry: './assets/js/index.js', // entry point of our app. assets/js/index.js should require other js modules and dependencies it needs
8+
entry: './assets/client/js/index.jsx', // entry point of our app. assets/client/js/index.jsx should require other js modules and dependencies it needs
99

1010
output: {
1111
path: path.resolve('./assets/bundles/'),

0 commit comments

Comments
 (0)