Git Product home page Git Product logo

sample-django-best-practices's Introduction

Django Best Practices

A simple Django codebase that provides best practices for a secure production deployment.

Status: WIP (not stable)


Checklist

Status Item info
✔️ Up-to-date Dependencies -
✔️ Simple Django Project -
✔️ BS5 for styling Local path (latest BS5 stable version)
✔️ Simple Custom Login / Register pages -
✔️ Unitary tests -
✔️ SCSS to CSS compilation -
✔️ Rate Limiter for Login & Register -
✔️ Page Compression -
✔️ Deployment -
✔️ HEROKU integration -
✔️ Docker -

✨ Build from sources

$ # Clone the sources
$ git clone https://github.com/app-generator/sample-django-best-practices.git
$ cd sample-django-best-practices
$
$ # Virtualenv modules installation (Unix based systems)
$ virtualenv env
$ source env/bin/activate
$
$ # Virtualenv modules installation (Windows based systems)
$ # virtualenv env
$ # .\env\Scripts\activate
$
$ # Install requirements
$ pip3 install -r requirements.txt
$
$ # Run the application
$ python manage.py makemigrations  
$ python manage.py migrate
$ python manage.py runserver

✨ Pre-compile and compressing styles files

These processes are automatically handled when the server is running. For this, we are using django packages such as:

✨ Build from docker

Before building the containers with Docker, create a .env file. You can check .env.sample file for the structure of the environment file. You will need to have a SECRET_KEY and DEBUG variables. You can check djecrety.

SECRET_KEY=
DEBUG=

And finally, build and run the containers. The Dockerfile.dev at the root of the project is used for the Django application.

docker-compose up --build -d

The application will be running at http://localhost:85

✨ Assets Management for production

Your assets may come a CDN. Find the STATIC_URL and MEDIA_URL variables and modify them according to your zone URL or Zone alias.

First of all, if you have configured routing for static and media files, make sure they are only used on DEBUG.

STATIC_URL = 'http://ZONE_URL/static/'
MEDIA_URL = 'http://ZONE_URL/media/'

if DEBUG:
   STATIC_URL = '/static/'
   MEDIA_URL = '/media/'

We are nearly done, but Django comes with its own static files. These files should be moved to the STATIC_ROOT folder.

python manage.py collectstatic

The command above will move the pull static files in the STATIC_ROOT.

✨ Testing

Tests in the projects are handled with pytest. To run the tests in this project, type the following command in the shell.

pytest

✨ Deploy on github workflow

For the deployment on Heroku, there no need to use docker-compose. Heroku just need a Dockerfile. You can find an example of a Dockerfile for Heroku deployment at the root of the project.

First set the git secrets using this https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository Add the following keys to the secrets : HEROKU_APP_NAME , HEROKU_EMAIL , HEROKU_API_KEY. Note that the test job will run before the deploy is triggered.

✨ Code-base structure

The project has a super simple structure, represented as below:

.dockerignore
.github
   |-- workflows
   |   |-- django.yaml
.gitignore
Dockerfile
Dockerfile.dev
LICENSE
README.md
apps
   |-- __init__.py
   |-- apps.py
   |-- authentication
   |   |-- __init__.py
   |   |-- admin.py
   |   |-- apps.py
   |   |-- forms.py
   |   |-- migrations
   |   |   |-- __init__.py
   |   |-- models.py
   |   |-- tests.py
   |   |-- urls.py
   |   |-- views.py
   |-- home
   |   |-- __init__.py
   |   |-- urls.py
   |   |-- views.py
   |-- static
   |   |-- font-awesome-4.7.0
   |   |   |-- HELP-US-OUT.txt
   |   |   |-- css
   |   |   |   |-- font-awesome.css
   |   |   |   |-- font-awesome.min.css
   |   |   |-- fonts
   |   |   |   |-- FontAwesome.otf
   |   |   |   |-- fontawesome-webfont.eot
   |   |   |   |-- fontawesome-webfont.svg
   |   |   |   |-- fontawesome-webfont.ttf
   |   |   |   |-- fontawesome-webfont.woff
   |   |   |   |-- fontawesome-webfont.woff2
   |   |   |-- less
   |   |   |   |-- animated.less
   |   |   |   |-- bordered-pulled.less
   |   |   |   |-- core.less
   |   |   |   |-- fixed-width.less
   |   |   |   |-- font-awesome.less
   |   |   |   |-- icons.less
   |   |   |   |-- larger.less
   |   |   |   |-- list.less
   |   |   |   |-- mixins.less
   |   |   |   |-- path.less
   |   |   |   |-- rotated-flipped.less
   |   |   |   |-- screen-reader.less
   |   |   |   |-- stacked.less
   |   |   |   |-- variables.less
   |   |   |-- scss
   |   |   |   |-- _animated.scss
   |   |   |   |-- _bordered-pulled.scss
   |   |   |   |-- _core.scss
   |   |   |   |-- _fixed-width.scss
   |   |   |   |-- _icons.scss
   |   |   |   |-- _larger.scss
   |   |   |   |-- _list.scss
   |   |   |   |-- _mixins.scss
   |   |   |   |-- _path.scss
   |   |   |   |-- _rotated-flipped.scss
   |   |   |   |-- _screen-reader.scss
   |   |   |   |-- _stacked.scss
   |   |   |   |-- _variables.scss
   |   |   |   |-- font-awesome.scss
   |   |-- img
   |   |   |-- Finished Website.png
   |   |   |-- b.png
   |   |   |-- background.jpg
   |   |   |-- background2.png
   |   |   |-- bootstrap2.png
   |   |   |-- features.png
   |   |-- scss
   |   |   |-- styles.scss
   |-- templates
   |   |-- accounts
   |   |   |-- home.html
   |   |   |-- login.html
   |   |   |-- register.html
   |   |-- includes
   |   |   |-- base.html
   |   |-- index.html
   |-- user
   |   |-- __init__.py
   |   |-- admin.py
   |   |-- apps.py
   |   |-- migrations
   |   |   |-- 0001_initial.py
   |   |   |-- __init__.py
   |   |-- models.py
   |   |-- tests.py
   |   |-- views.py
core
   |-- __init__.py
   |-- asgi.py
   |-- settings.py
   |-- urls.py
   |-- wsgi.py
docker-compose.yaml
manage.py
nginx
   |-- nginx.dev.conf
pytest.ini
requirements.txt
staticfiles
   |-- .gitkeep

Django Best Practices - Provided by AppSeed

sample-django-best-practices's People

Contributors

app-generator avatar koladev32 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

fairhopeweb

sample-django-best-practices's Issues

Assets Management for production

hello @koladev32

The project should provide a dedicated section on the README related to assets management for production.

  • How to use the project with a popular CDN service KeyCDN or Cloudinary
  • How to update the configuration
  • collect static ...

[ all steps )

Docker ports Update

Hello @koladev32

Please configure Docker to use the below ports:

  • 5005 for the Gunicorn (not exposed)
  • 85 for the Nginx (exposed)

As for all other AppSeed kits, once the user type docker-compose up --build the app should be LIVE at:

http://localhost:85

P.S. Usually ports 5000, and 80 are taken by other services.

ty!

Unitary Tests - No tests are executed

Hello @koladev32

Please add a minimal test suite for the existing features:

  • register ok
  • login ok
  • registration with existing email
  • registration using a week password
  • login using an unexisting user
  • login using the wrong password.

All test data should be deleted at the end of the test suite.

P.S. The tests section should be also mentioned on the README.

Ty!

Docker Exec - Fails for missing `.env`

Hello @koladev32

The Docker execution exits with error .env: The system cannot find the file specified.

Please add .env.sample with default values and associate comments to inform the user about it.

Also, README should be updated to mention the .env creation from .env.sample

Ty!

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.