My Wiki!

Django + React

1. Sources

2. Create new app

Activate virtualenv or pipenv (TODO).

pip install pipenv
pipenv shell

Create new app in existing project

# pipenv install django
# django-admin startproject backend
cd project
python manage.py startapp todo

We may want to move the app to certain subfolder:

  python manage.py startapp service_listing
  mv service_listing django_maxo36/    
  

Note: startapp appname subfolder/appname can be used to create app in specific available folder. However, if the subfolder is already added to syspath (manage.py), python understands that the module is already exist. So we have to move the app folder manually.

2.1 Migrate new app

Add app to installed APPs

  LOCAL_APPS = [ 
  "django_maxo36.users.apps.UsersConfig",
  "django_maxo36.service_listing.apps.ServiceListingConfig",
  # Your stuff: custom apps go here
  ]
  # https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
  INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

Run migration in virtualenv:

  
  python manage.py migrate
  python manage.py runserver
  

As we are developing with docker (django cookie-cutter):

  docker-compose -f local.yml run --rm django python manage.py makemigrations service_listing
  docker-compose -f local.yml run --rm django python manage.py migrate service_listing

2.2 Creating super user

  
  docker-compose -f local.yml run --rm django python manage.py createsuperuser
  

We need to activate the account at the first login. If Mailhog is configured we can do it as follow.

When developing locally you can go with MailHog for email testing provided use_mailhog was set to y on setup. To proceed,

make sure mailhog container is up and running; Open http://http://192.168.39.208:8025. (The IP of host node).

2.3 Develope the backend app

2.3.1 Testing the app

curl http://192.168.39.208:3000/api/users/

Login as admin and create auth-token.

Get the token:

  curl -X POST http://192.168.39.208:8000/auth-token/ -d "username=admin&password=admin"
  {"token":"d04508e7c5d70e8ac804af8041f8669af5b40d86"}

User auth-token to request:

  curl http://192.168.39.208:3000/api/users/ -H 'Authorization: Token d04508e7c5d70e8ac804af8041f8669af5b40d86' 
 
  [{"username":"admin","email":"admin@me.com","name":"","url":"http://192.168.39.208:3000/api/users/admin/"}]
  

Brows API: http://192.168.39.208:3000/api/servicelisting/

3. Develop frontend

3.1 Build system

3.1.1 Gulp-Browserify (not used)

  npm install --save-dev gulp-babel @babel/core @babel/preset-env @babel/preset-react @babel/preset-es2015 
  
  https://gist.github.com/thuydang/310109b0fdd7ca0a1a3546b8beecc06e

3.1.2 Content Security Policy

The build tools should bundle all resources and publish them. This will work with “self” CSP. A nice tool would create and inject nonce automatically. However, we haven't found one yet.

3.1.3 Gulp-Rollup-React

3.1.4 Handle css, fonts in node_modules

3.2 React Components

3.3 Next Steps

4. Troubleshooting

4.1 node container not starting

node            | Killed
node            | npm ERR! code ELIFECYCLE
node            | npm ERR! errno 137
node            | npm ERR! django_maxo36@0.1.0 dev: `gulp`
node            | npm ERR! Exit status 137

This error may be caused by lack of memory. Increase ram, e.g., in minikube.

Try docker-compose up until it is stable.

Check memory usage:

  docker stats --all
  docker stats node
  

5. Further learning material


Navigation