Skip to content

timokluser-dev/django-workshop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

django-workshop

➡️ Frontend: timokluser-dev/django-workshop-frontend

django logo

setup

  • Django v4 Backend
  • Django Debug Toolbar
  • Wagtail CMS
  • GraphQL API

create project

  • make init (will set hosts & create .env)
  • docker-compose run django bash
  • pip freeze (show dependencies)
  • pip install django
  • pip freeze (show dependencies)
  • pip freeze > requirements.txt
  • django-admin startproject app .
  • exit
  • .docker/Dockerfile (remove comment on line 38)
  • docker-compose up --build

debugging with PyCharm

: arrow_right: engineering-playbook/python/django_debug/debug.md

creating new models

file: db/models.py

  • build models
  • add __str__(self) method
  • (add Meta class)
# after model created / changed:
./manage.py  makemigrations

# apply the newly created migration [for app db]
./manage.py  migrate [db]

→ Make small migrations for better maintainability


file: db/admin.py

  • register model for django admin
    • admin.site.register(Model)

updating models

when doing changed to the models, pay attention to the following:

  • set default or null values for existing entries:
    • models.TextField(null=True, ...)
    • or
    • models.TextField(default="some defaults", ...)
  • when renaming attributes, do one migration only for renaming

Wagtail CMS

ℹ️ wagtail is currently not compatible with Django 4. It will perform a downgrade of Django to version 3 during install.

Login: http://django.what-ever.lo/cms/

pip install wagtail
pip install wagtailmedia
pip freeze > requirements.txt

➡️ https://docs.wagtail.io/en/stable/getting_started/integrating_into_django.html

→ add 'wagtailmedia' to INSTALLED_APPS in settings.py

Django Debug Toolbar

pip install django-debug-toolbar
pip install django-graphiql-debug-toolbar

➡️ https://django-debug-toolbar.readthedocs.io/en/latest/installation.html ➡️ https://pypi.org/project/django-graphiql-debug-toolbar/


when using docker:

file: app/settings.py

# ...

if DEBUG:
    hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
    INTERNAL_IPS = [ip[:-1] + '1' for ip in ips] + ['127.0.0.1', '10.0.2.2']


def show_debug_toolbar(request):
    return DEBUG


DEBUG_TOOLBAR_CONFIG = {
    'SHOW_TOOLBAR_CALLBACK': show_debug_toolbar,
}

Graphene

Library for GraphQL

pip install graphene-django

➡️ https://docs.graphene-python.org/projects/django/en/latest/installation/

Notes regarding GraphQL:

  • API Requests are always POST
  • API status code is always 200 (no 404)
    • check for response.data && !response.errors

JWT Authorization

➡️ https://django-graphql-jwt.domake.io/index.html

Default Permissions: https://docs.djangoproject.com/en/4.0/topics/auth/default/#default-permissions


Django template

Add Hosts file

Add the following to your /etc/hosts file.

127.0.0.1       django.what-ever.lo

Setup environment variables

make init

Docker shortcuts

make up
make down
make bash

Django Admin GUI

You can access the Admin Gui through http://django.what-ever.lo/admin/.

Username: admin
Password: admin