Skip to content

velasquezerik/start-django-project

Repository files navigation

Django Starter Project

This repo is a starting point for a clasic django web service project running on docker that have the following services:

  • web: Web server running django on port 8000.
  • worker: Run async tasks with celery.
  • redis: Used to store celery tasks.
  • nginx: So this nginx proxy server will help us handle requests and serving static files.

The database should be configured in the host machine, as it is easier for development.

Getting started

If you are starting a new project go ahead and clone this repo in a directory of your choosing

Create a database for your project. Then you need to create a file called .env and write the environment variables you wish to use for development

# -----------------------------------------------------------------------------
# Basic Config
# -----------------------------------------------------------------------------
ENV=dev
DEBUG=on
ROOT_URLCONF=conf.urls
WSGI_APPLICATION=conf.wsgi.application

# -----------------------------------------------------------------------------
# Time & Language
# -----------------------------------------------------------------------------
LANGUAGE_CODE=en-us
TIMEZONE=UTC
USE_I18N=on
USE_L10N=on

# -----------------------------------------------------------------------------
# Emails
# -----------------------------------------------------------------------------
[email protected]
EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend

# -----------------------------------------------------------------------------
# Security and Users
# -----------------------------------------------------------------------------
SECRET_KEY='django-insecure-ybeqypo0%0#9p8usxk1ifuieu#4e8wvsp=7o-ya%!p#_*zxkmb'
ALLOWED_HOSTS=web,localhost,127.0.0.1
CSRF_TRUSTED_ORIGINS=http://web:8000,http://localhost:8000,http://127.0.0.1:8000
LOGIN_URL=/accounts/login/
LOGIN_REDIRECT_URL=/

# -----------------------------------------------------------------------------
# Databases
# -----------------------------------------------------------------------------
DATABASE_URL=postgres://<user>:<password>@127.0.0.1:5432/<db_name>
DEFAULT_AUTO_FIELD=django.db.models.BigAutoField

# -----------------------------------------------------------------------------
# Celery
# -----------------------------------------------------------------------------
CELERY_BROKER_URL=redis://cache
CELERY_TASK_ALWAYS_EAGER=off

# -----------------------------------------------------------------------------
# Static & Media Files
# -----------------------------------------------------------------------------
STATIC_URL=/static/
MEDIA_URL=/media/

# -----------------------------------------------------------------------------
# Storage files
# -----------------------------------------------------------------------------
USE_STORAGE=off

# -----------------------------------------------------------------------------
# Sentry
# -----------------------------------------------------------------------------
USE_SENTRY=off
SENTRY_DSN=https://<project-key>@sentry.io/<project-id>


# -----------------------------------------------------------------------------
# allauth settings
# -----------------------------------------------------------------------------
ACCOUNT_AUTHENTICATION_METHOD=email
ACCOUNT_CHANGE_EMAIL=on
ACCOUNT_EMAIL_REQUIRED=on
ACCOUNT_USERNAME_REQUIRED=off
ACCOUNT_EMAIL_VERIFICATION=mandatory
SITE_ID=1
ACCOUNT_EMAIL_NOTIFICATIONS=on
ACCOUNT_LOGOUT_ON_GET=on
ACCOUNT_LOGOUT_ON_PASSWORD_CHANGE=on
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE=on
ACCOUNT_UNIQUE_EMAIL=on
ADMIN_LOGIN_USING_ALLAUTH=on

We now need to override DATABASE_URL environment variable inside of Docker to connect directly to you host machine. Create a file called .env.docker with the following content:

DATABASE_URL=postgres://<user>:<password>@<host>:5432/<db_name>
  • user is the user in your host machine that has access to postgres in this case.
  • password is the user password in your host machine that has access to postgres database.
  • host is the ip or address of your host machine.
  • db_name Database name.

We are all set up for bringing everything live with

docker-compose up

Wait for everything to load, and you can visit https://127.0.0.1:8000 and your new awesomely configured site will be there.

Docker commands

Here are a few commands that may come in handy

Command Description
docker ps List all containers (-a to include stopped)
docker logs --follow <container_id> Display the logs of a container
docker exec -it <container_id> /bin/bash Attach into a running container
docker run --rm <image_name> /bin/bash Run a docker container based on an image and get a prompt
docker-compose run --rm web /bin/bash Same as before but for services defined in docker-compose.yml
docker-compose run --rm web /bin/bash -c 'python manage.py migrate' Run a management command

Old fashion install

First of all, install pipenv so you can use the specified python version (check out .python-version). Then, run pip install pipenv to install it's pip's successor: pipenv. Then install dependencies by running pipenv install. You can now start developing.

These commands are at your disposal:

Command Shortcut for
pipenv run server python manage.py runserver
pipenv run prod-server gunicorn conf.wsgi:application --bind 0.0.0.0:8000
pipenv run dev-server python manage.py runserver
pipenv run tests pytest
pipenv run celery python manage.py celery_autoreload
pipenv run test-celery python manage.py command_test_celery
pipenv run shell python manage.py shell
pipenv run makemigrations python manage.py makemigrations
pipenv run migrate python manage.py migrate
pipenv run createsuperuser python manage.py createsuperuser
pipenv run static python manage.py collectstatic
pipenv run isort . isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections.
pipenv run flake8 Flake8 runs all the tools by launching the single flake8 command. It displays the warnings in a per-file, merged output.

To compile your static files, you need to run pipenv run static.

Environment variables

These environment variables can be provided to configure your project.

Django

Name Values Default Description
ENV dev, test, qa, prod prod Indicates in which environmet the project is running on
DEBUG on, off off Run server in debug mode
LANGUAGE_CODE Language Identifier (RFC 3066) en-US List of language codes
TIME_ZONE Record of IANA time zone database America/Santiago List of timezones
USE_I18N on, off on Enable translation system
USE_L10N on, off on Enable localized formatting
USE_TZ on, off on Enable timezone aware dates in
DEFAULT_FROM_EMAIL E-mail addresss -- Email address from which transactional emails will be sent from
EMAIL_BACKEND Path to backend django.core.mail.backends.smtp.EmailBackend The backend to use for sending emails. List of backends
SECRET_KEY Random string -- Used to provide cryptographic signing
ALLOWED_HOSTS List of domains -- Represents the host/domain names that this site can serve.
DJANGO_DATABASE_URL Database url -- Describes the database connection with a url strucure.
LOGIN_URL Url /login/ Url to redirect users when login is needed
LOGIN_REDIRECT_URL Url / Url to redirect users after login in
STATIC_URL Url /static/ Url from which static files are served
MEDIA_URL Url /media/ Url from which media files are served

Celery

Name Values Default Description
CELERY_BROKER_URL Database url -- A common value for development is to use redis://cache, but it's recommended for production to use RabbitMQ
CELERY_TASK_ALWAYS_EAGER on, off off If this is True, all tasks will be executed locally by blocking until the task returns.

Loggin & Sentry

Name Values Default Description
LOGS_ROOT path -- Path to the directory where logs are to be stored
USE_SENTRY on, off off Enables sentry
SENTRY_DSN string -- Private URL-like configuration

Allauth

Name Values Default Description
ACCOUNT_AUTHENTICATION_METHOD username, email, username_email email Specifies the login method to use – whether the user logs in by entering their username, email address, or either one of both.
ACCOUNT_CHANGE_EMAIL on, off on When enabled (True), users are limited to having exactly one email address that they can change by adding a temporary second email address that, when verified, replaces the current email address.
ACCOUNT_EMAIL_REQUIRED on, off on The user is required to hand over an email address when signing up.
ACCOUNT_USER_MODEL_USERNAME_FIELD string None The name of the field containing the username, if any. See custom user models.
ACCOUNT_USERNAME_REQUIRED on, off off The user is required to enter a username when signing up.
ACCOUNT_EMAIL_VERIFICATION mandatory, optional, none mandatory Determines the email verification method during signup
SITE_ID int 1 Site ID
ACCOUNT_EMAIL_NOTIFICATIONS on, off on When enabled, account related security notifications, such as “Your password was changed”, including information on user agent / IP address from where the change originated, will be emailed.
ACCOUNT_LOGOUT_ON_GET on, off on Determines whether or not the user is automatically logged out by a GET request.
ACCOUNT_LOGOUT_ON_PASSWORD_CHANGE on, off on Determines whether or not the user is automatically logged out after changing or setting their password.
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE on, off on When signing up, let the user type in their password twice to avoid typos
ACCOUNT_UNIQUE_EMAIL on, off on Enforce uniqueness of email addresses.
ADMIN_LOGIN_USING_ALLAUTH on, off on Enforce using allauth authentication for admin site.
USERSESSIONS_TRACK_ACTIVITY on, off on Whether or not user sessions are kept updated.

About

Repository guide to start a project in Django with Docker.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •