/django-tutorial

A sample django app that connects to MSSQL db (with docker containers instructions)

Primary LanguagePythonMIT LicenseMIT

django-tutorial

This project uses python 3.9,using other python version may cause some problems

 

Install package

 

install package by pip

pip install -r requirements/base.txt

 

if working for production, need to install production.txt

pip install -r requirements/production.txt

 


 

Install Microsoft ODBC driver

 

To connect sql server, we need to install microsoft ODBC driver This project uses ODBC Driver 13 for SQL Server

For windows

download windows installer and install

 

For Ubuntu

please follow steps from microsoft official web

 


 

Database settings

 

create .sql in root path, MSSQL_HOST and TEST_MSSQL_HOST can't be the same

 

.sql

MSSQL_HOST=<host>
MSSQL_NAME=djangoproject
MSSQL_USERNAME=<username>
MSSQL_PASSWORD=<password>
USE_MSSQL_FAKE_DATA=True

TEST_MSSQL_HOST=<host>
TEST_MSSQL_NAME=djangoproject
TEST_MSSQL_USERNAME=<username>
TEST_MSSQL_PASSWORD=<password>
USE_MSSQL_FAKE_DATA=True

 


 

Django command

 

Check if everything was correct

 

python manage.py check

 

Migrate

 

django has some default migrations that we need to migrate, so run command below

 

migrate to database

python manage.py migrate

 

(optional) if we create some apps, we need to create migrations for apps

 

create migrations

python manage.py makemigrations

 

migrate for specify app

python manage.py migrate app_name

 

migrate for specify app migrations number

python manage.py migrate app_name 0001

 

revert migrate

python manage.py migrate app_name zero

 

Shell

python manage.py shell

 

Create superuser

The sample views in sqlserver/views.py has a decorator of login_required, meaning you will need to login to see the page. Therefore, you will need to create an user. Yod could run python manage.py createsuperuser and follow the instructions to create a superuser to test the functionality of the app.

Run server

python manage.py runserver

 

run server for specify settings file

python manage.py runserver --settings=django_tutorial.settings_test

 

run server for specify settings ip and port

python manage.py runserver 0.0.0.0:8001

 

Testing

 

when testing, we need to specify settings file for testing

python manage.py test --settings=django_tutorial.settings_test