Django: Difference between revisions
Jump to navigation
Jump to search
Created page with "=Setting up REST API within Django= These are just notes from a demo =Create a Django Project= Follow steps on https://www.django-rest-framework.org/tutorial/quickstart/#proje..." |
|||
Line 54: | Line 54: | ||
pika==1.1.0 | pika==1.1.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Login to Container= | |||
Login to the container with | Login to the container with | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
docker-compose exec backend bash | docker-compose exec backend bash | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Create an App= | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
python manage.py startapp products | python manage.py startapp products | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Amend the Settings on the container= | |||
We need to modify the following sections | We need to modify the following sections | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
Line 99: | Line 99: | ||
CORS_ALLOW_ALL_ORIGINS = True | CORS_ALLOW_ALL_ORIGINS = True | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Create Model Classes= | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 11:35, 6 January 2021
Setting up REST API within Django
These are just notes from a demo
Create a Django Project
Follow steps on https://www.django-rest-framework.org/tutorial/quickstart/#project-setup
Create a Container
Docker Compose
We can build a container using docker-compose. Create a docker file with
FROM python:3.9
ENV PYTHONNUMBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY . /app
CMD python manage.py runserver 0.0.0.0:8000
Next write a yaml to install software
version: '3.8'
services:
backend:
build:
context: .
dockerfile: Dockerfile
ports:
- 8000:8000
volumes:
- .:/app
depends_on:
- db
db:
image: mysql:5.7.32
restart: always
environment:
MYSQL_DATABASE: admin
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
volumes:
- .dbdata:/var/lib/nysql
ports:
- 33066:33036
Create a requirements.txt document
Django==3.1.5
djangorestframework==3.12.2
mysqlclient==2.0.3
django-mysql==3.10.0
django-cors-headers==3.6.0
pika==1.1.0
Login to Container
Login to the container with
docker-compose exec backend bash
Create an App
python manage.py startapp products
Amend the Settings on the container
We need to modify the following sections
INSTALLED_APPS = [
...
'django.contrib.staticfiles',
'rest-framework',
'corsheaders',
'products'
]
MIDDLEWARE = [
...
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
...
]
...
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'admin',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'db',
'PORT': '3306'
}
}
...
CORS_ALLOW_ALL_ORIGINS = True