Giter VIP home page Giter VIP logo

django-rest-exercise's Introduction

Django-and-Django-REST-Framework

Setup

Installing docker compose

  1. Download the Docker compose package:

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

  1. Apply executable permissions to the binary:

sudo chmod +x /usr/local/bin/docker-compose

  1. Test the installation:

docker-compose --version

Buillding your Container

  1. Building your new image using your Dockerfile:

docker build -t my-django-image:your-image-tag .

  1. Open your docker-compose.yml file and change the 'XX' to your corresponding port. Also change 'YOUR_USER' with your UNIX username:
version: '3'
services:
  web:
    build: .
    command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:80XX"
    container_name: "djangoXX"
    volumes:
      - "/home/YOUR_USER/django/library:/usr/src/django/library"
    ports:
      - "90XX:80XX"
  1. Using docker-compose in order to deploy your container:

docker-compose up

Django development

  1. Create a new virtual environment:
python -m venv django_env
  1. Use this virtual env:
source ./django_env/bin/activate
  1. Install dependencies:
pip install django
pip install django_rest_framework
  1. Create a new project:
django-admin startproject library
  1. Create a new application of the project:
django-admin startapp editorial
  1. Run your migrations:
python manage.py migrate
  1. Add your application to the INSTALLED_APPS section:
# settings.py
INSTALLED_APPS = [
    ...
    'rest_framework',
    'editorial'
]
  1. Create the files serializers.py and urls.py inside library/editorial directory:
cd library/editorial
touch serializers.py urls.py

Solving your issues

  • Issue #1 Running docker-compose up shows "Database found: skipping initial configuration": Postgres images runs some configuration only the first time we build and run our container. In order for solving this issue we must stop our current container with CTRL C, then enter following commands:
docker-compose rm web
docker-compose rm postgres_db
docker-compose up --build
  • Issue #2 Password authentication failed for your user and also Issue #3 Connection refused, Is the server running on that host and accepting TCP/IP connections: These errors occurs when we are trying to access to an unavailable instance of Postgres or if we tried to access Postgres in some IP server that doesn't have Postgres expose on any port, we must use the right IP for postgres. We need to obtain our IP address of our network containers, in order for that we need to write the following commands:
# First we need to obtain our container ID
docker ps | grep YOUR_PORT

# Once you grep some instance with your related port, copy the ID 
# and paste it on the next command
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' YOUR_CONTAINER_ID

Then we need to use that IP address in our docker-compose.yml because it corresponds to the IP address of our Docker network.

Assignment

  1. Add between 2-4 models, obviously make their serializers, views and routers also; you can do it inside your book application (Our you can create another app inside our project with the command django-admin startapp your_app_name).
  2. Populate your book app and also your new models.
  3. Try your application with Postman (Do every HTTP method of every Model in our project: GET, POST, PUT DELETE and PATCH).
  • Tip: If you got some issues to send your Http Request you should check out the Django and Django REST Frameworkd documentation.

References

django-rest-exercise's People

Contributors

hazemsr avatar

Stargazers

Joel Isaias Solano Ocampo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.