page_type | languages | products | description | urlFragment | ||
---|---|---|---|---|---|---|
sample |
|
|
This repo contains tested reference examples of using Django with SQL Servers. |
mssql-django-samples |
Connect to SQL Database from Django app:
- Download Python installer.
Install Python3 if your machine does not have it. Visit Python download page. - Install and verify Python.
a. Double click the installer, follow instructions on the screen.
b. After finished install. Runpy -V
in command line to verify it.
> py -V
Python 3.7.8
- Install django and mssql-django
Use pip to install mssql-django,
> py -m pip install django mssql-django
Connect to SQL Database from Django app:
-
Install Python3
# eg. For Ubuntu
$ sudo apt-get install python3
- Install django and mssql-django
$ python3 -m pip install django mssql-django
- Create a Django project
$ django-admin startproject mysite
- Edit
setting.py
file
Go to mysite/mysite/settings.py
InDATABASE
section, edit accrodingly. Make sure theDATABASE_NAME
is exist in your SQL Server instance.
# settings.py
DATABASES = {
"default": {
"ENGINE": "mssql",
"NAME": "DATABASE_NAME",
"USER": "USER_NAME",
"PASSWORD": "PASSWORD",
"HOST": "HOST_ADDRESS",
"PORT": "1433",
"OPTIONS": {"driver": "ODBC Driver 17 for SQL Server",
},
},
}
- Run Django project
# Linux
$ python manage.py runserver
# Windows
\> py manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
April 22, 2021 - 17:48:06
Django version 3.1.8, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Now the development server is running at http://127.0.0.1:8000/. Open browser and navigate to that page, you should be able to see a Django welcome page.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.