Skip to content

Basic Django app template for AWS EC2 Deployment guide

Notifications You must be signed in to change notification settings

IFPE-Paulista-Rodrigo/django-app

 
 

Repository files navigation

Django Logo

Django App

This repository contains a simple Django app as part of the Deploying a Production ready Django App on EC2 with CI/CD gist tutorial which you can find here

Table of Contents

Project Layout

Here is the project layout:

django-app
  |___ backend/ (Django Backend settings)
  |   |___ settings.py
  |___ static_files/
  |___ templates/ (Django Templates)
  |   |___ index.html
  |___ scripts/
  |___ manage.py
  |___ requirements.txt
  |___ Dockerfile
  |___ docker-compose.yml

The default settings are typically built for development rather than production and I found it difficult to change my settings after building the project. My inspiration comes from this article here: https://djangostars.com/blog/configuring-django-settings-best-practices/ where the different approaches are listed.

My approach uses the django-environ package (https://django-environ.readthedocs.io/en/latest/#) which makes it relatively easy to manage your development and production environment variables.

I have also included the django-debug-toolbar (https://django-debug-toolbar.readthedocs.io/en/latest/) which I found useful in debugging and optimizing Django, specifically when it came to how my app queries the database.

Note: Make sure to add the .env files to your .gitignore. They are not included by default in this project so that you have a reference to what type of data should be in there.

Requirements

  • Python 3.7+
  • Django 3.0+

Setting up the Django-app project with Docker

For those that are not interested in setting up the project manually or would simply not have to worry about downloading python and its dependencies, I have created a Dockerfile and docker-compose.yml file to help create a container with everything you would need to run the django-app.

Install Docker

To make this as easy as possible, we will be using Docker Compose to creat our container.

$ docker-compose --version
docker-compose version 1.26.2, build eefe0d31

Build and Run the Container

  • Clone the repo to your local machine:
$ git clone https://github.com/rmiyazaki6499/django-app.git
  • Go into the project directory to build and run the container with:
$ cd django-app/
$ docker-compose up --build

Navigate to http://localhost:8000 to view the site on the local server. It should look something like this:

django-default

Cleaning up the Container and Image

To stop the container from running, use <Ctrl-C> twice. To close down the container use the command:

$ docker-compose down

Then to clean up the container and image which we are no longer using use the command:

$ docker system prune -fa

Confirm that the container and image is no longer there with:

$ docker system df -v

Setting up the Django-app project manually

If you either did not want to use Docker or was curious to build the django-app manually follow the directions below.

  • On your terminal and clone the repository with Git:
$ git clone https://github.com/rmiyazaki6499/django-app.git
  • Next, go into the project directory and make sure you create a virtual environment for your project by either using venv or pipenv:
$ cd django-app/
$ python3 -m venv env
$ source env/bin/activate
$ pip3 install -r requirements.txt
  • We will now migrate the database and collect the static files:
$ python3 manage.py makemigrations
$ python3 manage.py migrate
$ python3 manage.py collectstatic
  • To run the development server, use the following command:
$ python3 manage.py runserver
  • To run the production server, use the following command:
$ ENV_PATH=.env-prod python3 manage.py runserver

Navigate to http://localhost:8000 to view the site on the local server. It should look something like this:

django-default

Author

Created by:

About

Basic Django app template for AWS EC2 Deployment guide

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 75.9%
  • Shell 15.8%
  • Dockerfile 8.3%