Djangoprojekt strukturieren: https://docs.python-guide.org/writing/structure/#regarding-django-applications
mkdir project
cd project
django-admin startproject mysite .
Development Server:
python manage.py runserver
Die Webseite sollte hier verfuegbar sein: http://127.0.0.1:8000/
python manage.py startapp appname
- Models aktivieren
<https://docs.djangoproject.com/en/2.1/intro/tutorial02/#activating-models>
- Models migrieren:
```bash
python manage.py makemigrations appname
python manage.py sqlmigrate polls 0001 # view the SQL code
python manape.py migrate
```
- Admin erstellen
```bash
python manage.py createsuperuser
```
<https://docs.djangoproject.com/en/2.1/intro/tutorial02/#make-the-poll-app-modifiable-in-the-admin>
https://docs.djangoproject.com/en/2.1/intro/tutorial02/#playing-with-the-api
python manage.py shell
https://stackoverflow.com/questions/47170049/run-django-shell-in-ipython
python manage.py shell -i ipython
Dotenv
- Secret Managementpip install python-dotenv
Generate a SECRET_KEY:
python manage.py shell -c 'from django.core.management import utils; print(utils.get_random_secret_key())'
Use the package python-dotenv
.
Create .env
file:
KEY=VALUE
import os
from dotenv import load_dotenv
# Load environment
load_dotenv()
KEY = os.getenv('KEY', 'default')
KEY=os.environ['KEY']