Example Python Django application and CI/CD pipeline for integrating it with GitHub CI/CD. This application demonstrates CRUD operations using class based views in Django. It also includes UI for all CRUD views. And it's based on this tutorial: https://semaphoreci.com/blog/python-continuous-integration-continuous-delivery.
-
Use virtualenv for setting up this project
-
Install pip requirements
pip install -r requirements.txt -
Create new psql database
postgres=# create database pydjango; -
Setup your database credentials and
SITE_URLin settings.py file available inside ### core folder -
Once you have setup your database, Open command prompt pointing to the Root of the project directory and run following command to create application default database
(virtualenv / conda environment) > python manage.py migrate (virtualenv / conda environment) > python manage.py createsuperuser -
Once all of the above command run sucessfully, We are ready to go. Start server by executing command
(virtualenv / conda environment) > python manage.py runserverand visit the web browser with 'http://127.0.0.1:8000'
The following environment variables can be set to override defaults:
SECRET_KEY: Django secret key.DB_ENGINE: Django database backend.DB_NAME: database name.DB_HOST: database hostname.DB_PORT: database port.DB_USER: database user.DB_PASSWORD: database password.
The example pipeline contains 3 blocks:
- Install Dependencies
- Installs pip requirements
- Run Code Analysis
- Run code analysis / code linting with Pylint
- Run Unit Tests
- Runs Unit Tests with pytest module for views and models file
To tell pytest witch Django settings that should be used for tests runs, we need to setup a pytest configuration file, called pytest.ini in our project root directory. The file contains:
[pytest]
DJANGO_SETTINGS_MODULE=core.test_settings
addopts = --nomigrations --cov=. --cov-report=html
To run the tests, we can invoke directly pytest command instead of manage.py test.
The pytest-django is designed to run with the pytest command, but in case of you want to use manage.py test with pytest-django, you can create a simple test runner.