Skip to content

Commit 0611a16

Browse files
committed
✨ add react to the project
1 parent 89a86d7 commit 0611a16

File tree

11 files changed

+140
-18
lines changed

11 files changed

+140
-18
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env", "@babel/preset-react"]
3+
}

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77
/node_modules/
88
build/
99
env
10+
venv/
11+
12+
# ignore compiled files
13+
14+
frontend/static/
1015

1116
#src
1217
*.sqlite*
1318

19+
# json files
20+
webpack-stats.json
21+

core/settings.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,29 @@
2626
DEBUG = True
2727

2828
# Hosts Settings
29-
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '.onrender.com', '0.0.0.0']
30-
CSRF_TRUSTED_ORIGINS = ['http://localhost:8000', 'http://localhost:5085', 'http://127.0.0.1:8000', 'http://127.0.0.1:5085', 'https://core-django.onrender.com']
29+
ALLOWED_HOSTS = ["localhost", "127.0.0.1", ".onrender.com", "0.0.0.0"]
30+
CSRF_TRUSTED_ORIGINS = [
31+
"http://localhost:8000",
32+
"http://localhost:5085",
33+
"http://127.0.0.1:8000",
34+
"http://127.0.0.1:5085",
35+
"https://core-django.onrender.com",
36+
]
3137

3238
# Application definition
3339

3440
INSTALLED_APPS = [
41+
"webpack_loader",
42+
"frontend",
43+
"admin_datta.apps.AdminDattaConfig",
3544
"django.contrib.admin",
3645
"django.contrib.auth",
3746
"django.contrib.contenttypes",
3847
"django.contrib.sessions",
3948
"django.contrib.messages",
4049
"django.contrib.staticfiles",
41-
4250
# APPS
4351
"home",
44-
4552
# Util
4653
"debug_toolbar",
4754
]
@@ -55,14 +62,13 @@
5562
"django.contrib.auth.middleware.AuthenticationMiddleware",
5663
"django.contrib.messages.middleware.MessageMiddleware",
5764
"django.middleware.clickjacking.XFrameOptionsMiddleware",
58-
5965
# Util
6066
"debug_toolbar.middleware.DebugToolbarMiddleware",
6167
]
6268

6369
ROOT_URLCONF = "core.urls"
6470

65-
UI_TEMPLATES = os.path.join(BASE_DIR, 'templates')
71+
UI_TEMPLATES = os.path.join(BASE_DIR, "templates")
6672

6773
TEMPLATES = [
6874
{
@@ -130,13 +136,18 @@
130136

131137
STATIC_URL = "static/"
132138

133-
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
139+
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
134140

135-
STATICFILES_DIRS = (
136-
os.path.join(BASE_DIR, 'static'),
137-
)
141+
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
138142

139143
# Default primary key field type
140144
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
141145

142146
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
147+
148+
WEBPACK_LOADER = {
149+
"DEFAULT": {
150+
"BUNDLE_DIR_NAME": "frontend/",
151+
"STATS_FILE": os.path.join(BASE_DIR, "webpack-stats.json"),
152+
}
153+
}

core/urls.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@
1919
urlpatterns = [
2020
path("", include("home.urls")),
2121
path("admin/", admin.site.urls),
22-
path("__debug__/", include("debug_toolbar.urls")),
22+
path("__debug__/", include("debug_toolbar.urls")),
23+
24+
path("", include("admin_datta.urls")),
2325
]

frontend/src/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from "react";
2+
import ReactDOM from "react-dom/client";
3+
4+
export default function App() {
5+
return (
6+
<div>
7+
<h1>This is a react Application</h1>
8+
</div>
9+
);
10+
}
11+
12+
const root = ReactDOM.createRoot(document.getElementById("app"));
13+
root.render(<App />);

gunicorn-cfg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
Copyright (c) 2019 - present AppSeed.us
44
"""
55

6-
bind = '0.0.0.0:5005'
6+
bind = "0.0.0.0:5005"
77
workers = 1
8-
accesslog = '-'
9-
loglevel = 'debug'
8+
accesslog = "-"
9+
loglevel = "debug"
1010
capture_output = True
1111
enable_stdio_inheritance = True

home/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
urlpatterns = [
66
path("", views.index, name="index"),
7+
path('frontend', views.frontend, name='frontend'),
78
]

home/views.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
from .models import *
88

9+
910
def index(request):
11+
context = {
12+
"date": datetime.today().strftime("%Y-%m-%d %H:%M:%S"),
13+
}
14+
return HttpResponse("Hello DJANGO! time is: " + context["date"])
1015

11-
context = {
12-
'date': datetime.today().strftime('%Y-%m-%d %H:%M:%S'),
13-
}
14-
return HttpResponse("Hello DJANGO! time is: " + context['date'])
16+
def frontend(request):
17+
return render(request, 'frontend/index.html')

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{
3+
"name": "django-react-starter",
4+
"version": "1.0.1",
5+
"description": "Starter repo for serving a React SPA from Django",
6+
"main": "index.js",
7+
"scripts": {
8+
"dev": "webpack --config webpack.config.js --watch --mode development",
9+
"build": "webpack --config webpack.config.js --mode production"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC",
14+
"dependencies": {
15+
"prop-types": "^15.8.1",
16+
"react": "^18.2.0",
17+
"react-dom": "^18.2.0",
18+
"react-router-dom": "^6.10.0",
19+
"reactflow": "^11.7.0",
20+
"recharts": "^2.5.0"
21+
},
22+
"devDependencies": {
23+
"@babel/core": "^7.20.12",
24+
"@babel/preset-env": "^7.20.2",
25+
"@babel/preset-react": "^7.18.6",
26+
"babel-loader": "^9.1.2",
27+
"clean-webpack-plugin": "^4.0.0",
28+
"css-loader": "^6.7.3",
29+
"style-loader": "^3.3.1",
30+
"webpack": "^5.75.0",
31+
"webpack-bundle-tracker": "^1.8.0",
32+
"webpack-cli": "^5.0.1"
33+
}
34+
}

templates/frontend/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!-- templates/frontend/index.html -->
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>React in Django</title>
7+
{% load render_bundle from webpack_loader %}
8+
{% render_bundle 'frontend' %}
9+
</head>
10+
<body>
11+
<div id="react-root">This is a React application in Django.</div>
12+
</body>
13+
</html>

webpack.config.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const path = require("path");
2+
const BundleTracker = require("webpack-bundle-tracker");
3+
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
4+
5+
module.exports = {
6+
entry: {
7+
frontend: "./frontend/src/index.js",
8+
},
9+
output: {
10+
path: path.resolve("./frontend/static/frontend/"),
11+
filename: "main.bundle.js",
12+
publicPath: "/static/frontend/",
13+
},
14+
plugins: [
15+
new CleanWebpackPlugin(),
16+
new BundleTracker({
17+
path: __dirname,
18+
filename: "./webpack-stats.json",
19+
}),
20+
],
21+
module: {
22+
rules: [
23+
{
24+
test: /\.js$/,
25+
exclude: /node_modules/,
26+
use: ["babel-loader"],
27+
},
28+
{
29+
test: /\.css$/,
30+
use: ["style-loader", "css-loader"],
31+
},
32+
],
33+
},
34+
};

0 commit comments

Comments
 (0)