Django

From Sinfronteras
Jump to: navigation, search

https://www.djangoproject.com/


Udemy course: Build a Backend REST API with Python Django - Advanced
In this course we build a Recipe REST API
https://www.udemy.com/course/django-python-advanced/


API Features:

  • 19 API Endpoints
Managing users, recipes, tags, ingredients
  • User Authentication
  • Browseable Admin Interface (Django Admin)
  • Browsable API (Swagger)



Techologies used in this course:

  • Django allows handle: URL Mappings / Object Relational Mapper / Admin site
  • Django REST Framework: Django add-on / Build REST APIs
  • PostgresSQL
  • Docker
  • Git
  • Swagger: Documentation / Browsable API (testing)



Structure of the project:

  • app/ : Main app - Django project
  • app/core/ : Hangle any code shared between multiple apps. Such as the DB definition using the Django modules
  • app/user/ : User related code. Such as User registration & auth tokens
  • app/recipe/ : Recipe related code. Such as handling updating ingredients - Creating/Deleting/Udating recipes



Unit Tests and Test-driven development (TDD)

See explanation at https://www.udemy.com/course/django-python-advanced/learn/lecture/32238668#notes


  • Unit Tests: Code which test code:
It's usually done this way:
  • You Set up some conditions; such as inputs to a function
  • Then you run a piece of code
  • Checks outputs of that code using "assertions"


Test-driven development (TDD)

... pegar la imagen de la presentacion que tomé del curso ...



Docker

Some benefits of using Docker Docker#Why use Docker

Drawbacks of using Docker in this project: (Not sure if these limitations are still in place)

  • VSCode will be unable to access interpreter
  • More Difficult to use integrated features



How will be used Docker in this project:

  • Define a Dockerfile: Contains all the OS level dependencies that our project needs.
  • Create a Docker Compose configuration: Tells Docker how to run the images that are created from our Docker file configuration.
  • Run all commands via Docker Compose. For example:
docker-compose run --rm app sh -c "python manage.py collectstatic"
  • docker-compose runs a Docker Compose command
  • run will start a specific container defined in config
  • --rm removes the container
  • app is the name of the service
  • sh -c pass in a shell command



Docker Hub

https://hub.docker.com/ Docker Hub is a cloud-based registry service that allows developers to store, share, and manage Docker images. It is a central repository of Docker images that can be accessed from anywhere in the world, making it easy to distribute and deploy containerized applications.

Using Docker Hub, you can upload your Docker images to a central repository, making it easy to share them with other developers or deploy them to production environments. Docker Hub also provides a search function that allows you to search for images created by other developers, which can be a useful starting point for building your own Docker images.

Docker Hub supports both automated and manual image builds. With automated builds, you can connect your GitHub (using Docker on GitHub Actions) or Bitbucket repository to Docker Hub and configure it to automatically build and push Docker images whenever you push changes to your code. This can help streamline your CI/CD pipeline and ensure that your Docker images are always up-to-date.


Docker Hub has introduced rate limits. Because of the rate limits, we have to Authenticate with Docker Hub: Create an account / Setup credentials / Login before running job.



Docker on GitHub Actions

Docker on GitHub Actions is a feature that allows developers to use Docker containers for building and testing their applications in a continuous integration and delivery (CI/CD) pipeline on GitHub.

With Docker on GitHub Actions, you can define your build and test environments using Dockerfiles and Docker Compose files, and run them in a containerized environment on GitHub's virtual machines. This provides a consistent and reproducible environment for building and testing your applications, regardless of the host operating system or infrastructure.

Docker on GitHub Actions also provides a number of pre-built Docker images and actions that you can use to easily set up your CI/CD pipeline. For example, you can use the "docker/build-push-action" action to build and push Docker images to a container registry, or the "docker-compose" action to run your application in a multi-container environment.

Using Docker on GitHub Actions can help simplify your CI/CD pipeline, improve build times, and reduce the risk of deployment failures due to environmental differences between development and production environments.




Setting the development environment

  • requirements.txt
Django>=3.2.4,<3.3
djangorestframework>=3.12.4,<3.13



  • Dockerfile
---
name: Checks

on: [push]

jobs:
  test-lint:
    name: Test and Lint
    runs-on: ubuntu-20.04
    steps:
      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Checkout
        uses: actions/checkout@v2
      - name: Test
        run: docker-compose run --rm app sh -c "python manage.py test"
      - name: Lint
        run: docker-compose run --rm app sh -c "flake8"



  • .dockerignore
# Git
.git
.gitignore

# Docker
.docker

# Python
app/__pycache__/
app/*/__pycache__/
app/*/*/__pycache__/
app/*/*/*/__pycache__/
.env/
.venv/
venv/



  • Then we create the app directory insde our project directory
mkdir app



  • Then we run:

docker build .



  • docker-compose.yml
version: "3.9"

services:
  app:
    build:
      context: .
      args:
        - DEV=true
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: >
      sh -c "python manage.py runserver 0.0.0.0:8000"



  • Then we run in our project directory:
docker-compose build



  • ...:
docker-compose run --rm app sh -c "flake8"

docker-compose run --rm app sh -c "django-admin startproject app ."

docker-compose up