### Django Full Stack Development on Windows with MSSQL Server
As a seasoned Django full stack developer on Windows, I'm excited to share my experience with you. I'll outline the steps to set up a Django project with MSSQL Server as the database.
**Prerequisites**
* Windows OS (I'm using Windows 10)
* Python 3.x installed (I'm using Python 3.9)
* pip package manager
* Microsoft ODBC Driver for SQL Server (download and install)
* MSSQL Server (you can use the Express edition for development)
**Step 1: Install required packages**
Open a new command prompt or PowerShell window and run the following commands:
```
pip install django
pip install pyodbc
```
**Step 2: Create a new Django project**
Run the following command to create a new Django project:
```
django-admin startproject myproject
```
This will create a new directory `myproject` with the basic Django project structure.
**Step 3: Configure MSSQL Server**
Create a new database in MSSQL Server, e.g., `mydatabase`. Note down the server name, database name, username, and password.
**Step 4: Configure Django to use MSSQL Server**
In the `myproject` directory, open the `settings.py` file and add the following code:
```python
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'mydatabase',
'USER': 'your_username',
'PASSWORD': 'your_password',
'HOST': 'your_server_name',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}
```
Replace the placeholders with your actual MSSQL Server credentials.
**Step 5: Create a new Django app**
Run the following command to create a new Django app:
```
python manage.py startapp myapp
```
This will create a new directory `myapp` with the basic app structure.
**Step 6: Define models and perform database migration**
In the `myapp` directory, open the `models.py` file and define your models. For example:
```python
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
```
Run the following commands to create and apply the database migration:
```
python manage.py makemigrations myapp
python manage.py migrate
```
**Step 7: Run the development server**
Run the following command to start the Django development server:
```
python manage.py runserver
```
Open your web browser and navigate to `http://localhost:8000/` to see the Django welcome page.
That's it! You now have a Django full stack project up and running on Windows with MSSQL Server as the database.
### Tips and Variations
* Make sure to adjust the `DATABASES` settings according to your specific MSSQL Server configuration.
* You can use other MSSQL Server versions, such as SQL Server 2017 or SQL Server 2019.
* For production environments, consider using a robust database connection pooling mechanism, such as `django-pyodbc-azure`.
* If you encounter issues with the ODBC driver, try updating to the latest version or using a different driver.