Difference between revisions of "Django"

From Sinfronteras
Jump to: navigation, search
Line 8: Line 8:
  
  
 +
<br />
 
'''API Features:'''
 
'''API Features:'''
 
* 19 API Endpoints
 
* 19 API Endpoints
Line 16: Line 17:
  
  
 +
<br />
 
'''Techologies used in this course''':
 
'''Techologies used in this course''':
* Django allows handle:
+
* Django allows handle: URL Mappings / Object Relational Mapper / Admin site
:* URL Mappings
+
* Django REST Framework: Django add-on / Build REST APIs
:* Object Relational Mapper
 
:* Admin site
 
 
 
* Django REST Framework:
 
:* Django add-on
 
:* Build REST APIs
 
 
 
 
* PostgresSQL
 
* PostgresSQL
 
* Docker
 
* Docker
* Swagger:
+
* Git
:* Documentation
+
* Swagger: Documentation / Browsable API (testing)
:* Browsable API (testing)
 
  
  
 +
<br />
 
'''Structure of the project:'''
 
'''Structure of the project:'''
 
* '''app/''' : Main app - Django project
 
* '''app/''' : Main app - Django project
Line 40: Line 35:
  
  
'''Unit Tests and Test-driven development (TDD):'''<br />
+
<br />
 +
==Unit Tests and Test-driven development (TDD)==
 
See explanation at https://www.udemy.com/course/django-python-advanced/learn/lecture/32238668#notes
 
See explanation at https://www.udemy.com/course/django-python-advanced/learn/lecture/32238668#notes
  
Line 56: Line 52:
  
  
'''How we'll use Docker:'''
+
<br />
 +
==Docker==
 
* Define a Dockerfile: Contains all  the OS level dependencies that our project needs.
 
* 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.
 
* Create a Docker Compose configuration: Tells Docker how to run the images that are created from our Docker file configuration.
Line 62: Line 59:
  
  
* Docker on GitHub Actions:
+
* '''Docker on GitHub Actions:'''
 
:* '''Docker Hub''' is where we can pull shared public images to reuse them for your project. For example there an image for Python, PostgreSQL, etc. Docker Hub has introduced rate limits.
 
:* '''Docker Hub''' is where we can pull shared public images to reuse them for your project. For example there an image for Python, PostgreSQL, etc. 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.
 
::* Because of the rate limits, we have to Authenticate with Docker Hub: Create an account / Setup credentials / Login before running job.
Line 70: Line 67:
 
----
 
----
  
* create «requirements.txt» in your project directory:
+
* '''create «requirements.txt» in your project directory:'''
 
+
<blockquote>
requirements.txt:
+
requirements.txt:
 +
<syntaxhighlight lang="html">
 
Django>=3.2.4,<3.3
 
Django>=3.2.4,<3.3
 
djangorestframework>=3.12.4,<3.13
 
djangorestframework>=3.12.4,<3.13
 +
</syntaxhighlight>
 +
</blockquote>
  
  
Line 156: Line 156:
  
 
docker-compose up
 
docker-compose up
 +
 +
 +
* ''' :'''
 +
<blockquote>
 +
<syntaxhighlight lang="html">
 +
 +
</syntaxhighlight>
 +
</blockquote>
 +
 +
 +
* ''' :'''
 +
<blockquote>
 +
<syntaxhighlight lang="html">
 +
 +
</syntaxhighlight>
 +
</blockquote>

Revision as of 12:50, 5 May 2023

https://www.djangoproject.com/



Recipe REST API - Udemy course

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

  • 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.


  • Docker on GitHub Actions:
  • Docker Hub is where we can pull shared public images to reuse them for your project. For example there an image for Python, PostgreSQL, etc. 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.
  • https://hub.docker.com/



  • create «requirements.txt» in your project directory:

requirements.txt:

Django>=3.2.4,<3.3
djangorestframework>=3.12.4,<3.13


Dockerfile: FROM python:3.9-alpine3.13 LABEL maintainer="adeloaleman"

ENV PYTHONUNBUFFERED 1 COPY ./requirements.txt /tmp/requirements.txt COPY ./app /app WORKDIR /app EXPOSE 8000

RUN python -m venv /py && \

   /py/bin/pip install --upgrade pip && \
   /py/bin/pip install -r /tmp/requirements.txt && \
   rm -rf /tmp && \
   adduser \
       --disabled-password
       --no-create-home \
       django-user
       

ENV PATH="/py/bin:$PATH"

USER django-user


.dockerignore:

  1. Git

.git .gitignore

  1. Docker

.docker

  1. Python

app/__pycache__/ app/*/__pycache__/ app/*/*/__pycache__/ app/*/*/*/__pycache__/ .env/ .venv/ venv/


Then we create the app directory insde our project directory

Then we run: docker build .


Then we create our docker-compose.yml: version: "3.9"

services:

 app:
   build:
     context: .
   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


  •  :


  •  :