# ✅ Windows Configuration Summary

**Project**: BMC Website Django  
**Configuration Date**: April 18, 2026  
**Status**: ✅ Ready for Windows Development  

---

## 📋 What Was Changed

### 1. Database Configuration

**File**: `BMC/settings.py`

#### ❌ BEFORE (PostgreSQL - Production)
```python
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'bmc',
        'USER': 'admin',
        'PASSWORD': '123@456a',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
```

#### ✅ AFTER (SQLite - Development)
```python
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# PostgreSQL commented out for production use
```

**Benefits**:
- ✅ No PostgreSQL server needed
- ✅ No complex setup
- ✅ File-based database (db.sqlite3)
- ✅ Perfect for Windows development
- ✅ Can switch back to PostgreSQL by uncommenting

---

### 2. Path Configuration

**File**: `BMC/settings.py`

#### ❌ BEFORE (Mixed Path Styles)
```python
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
LOCALE_PATHS = [
    BASE_DIR / 'locale/',  # Path object
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
```

#### ✅ AFTER (Consistent Windows Paths)
```python
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
LOCALE_PATHS = [
    os.path.join(BASE_DIR, 'locale'),  # String path
]
```

**Benefits**:
- ✅ Consistent path style across Windows
- ✅ No issues with Path objects on Windows
- ✅ Fully compatible with Windows file system
- ✅ Works with both staticfiles and media files

---

### 3. ALLOWED_HOSTS Configuration

**File**: `BMC/settings.py`

#### ❌ BEFORE (Production Only)
```python
ALLOWED_HOSTS = [
    "bmcgroup.com.vn",
    "www.bmcgroup.com.vn"
]
```

#### ✅ AFTER (Development + Production)
```python
ALLOWED_HOSTS = [
    "bmcgroup.com.vn",
    "www.bmcgroup.com.vn",
    "localhost",
    "127.0.0.1",
    "127.0.0.1:8000",
]
```

**Benefits**:
- ✅ Can access http://localhost:8000
- ✅ Can access http://127.0.0.1:8000
- ✅ Still works for production domains
- ✅ Easy development on Windows

---

## 📁 New Files Created

### 1. WINDOWS_DEV_SETUP.md (This File)
- Complete Windows development setup guide
- Troubleshooting section
- Common development tasks
- IDE setup (VS Code)

### 2. .env.example
- Environment variables template
- Shows format for configuration
- Can be copied to .env for actual values
- Should be in .gitignore

### 3. requirements.txt
- All Python dependencies listed
- Install with: `pip install -r requirements.txt`
- Easy to manage versions
- Can be used for both dev and prod

### 4. run_dev.bat
- Windows batch script
- Activates venv and runs server
- Interactive menu for common tasks
- No command line knowledge needed

### 5. run_dev.ps1
- PowerShell version of launcher
- Better for modern Windows users
- Color-coded output
- Same functionality as .bat

---

## 🚀 Quick Start Commands

### First Time Setup (Windows PowerShell or CMD)
```bash
# 1. Navigate to project
cd d:\Huy\Project\programing\website-bmc

# 2. Create virtual environment
python -m venv venv

# 3. Activate virtual environment
# For CMD:
venv\Scripts\activate

# For PowerShell:
venv\Scripts\Activate.ps1

# 4. Install dependencies
pip install -r requirements.txt

# 5. Run migrations
python manage.py migrate

# 6. Create admin user
python manage.py createsuperuser

# 7. Collect static files
python manage.py collectstatic --noinput

# 8. Start development server
python manage.py runserver
```

### Easy Launcher (Windows)
```bash
# Option 1: Double-click run_dev.bat
run_dev.bat

# Option 2: Run from PowerShell
.\run_dev.ps1

# Option 3: Run from command line
python manage.py runserver
```

---

## 📊 Configuration Matrix

| Aspect | Development (Windows) | Production (Ubuntu/Linux) |
|--------|----------------------|--------------------------|
| **Database** | SQLite (db.sqlite3) | PostgreSQL |
| **DEBUG** | True | False |
| **Server** | Django dev server | Nginx/Apache/Gunicorn |
| **HTTPS** | Not required | Required |
| **Static** | Served by Django | Served by web server |
| **Media** | Local filesystem | S3/Cloud storage |
| **Allowed Hosts** | localhost, 127.0.0.1 | Domain names only |
| **Setup Time** | ~5 minutes | ~30 minutes |
| **Users** | Single developer | Multiple concurrent |

---

## 🔄 Switching Between Dev and Production

### Development → Production

**Edit BMC/settings.py**:
```python
# Comment out SQLite
# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': BASE_DIR / 'db.sqlite3',
#     }
# }

# Uncomment PostgreSQL
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'bmc',
        'USER': 'admin',
        'PASSWORD': '123@456a',  # Use .env instead!
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
```

**Install PostgreSQL driver**:
```bash
pip install psycopg2-binary
```

**Migrate database**:
```bash
python manage.py migrate
```

### Production → Development

**Edit BMC/settings.py**:
```python
# Uncomment SQLite
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# Comment out PostgreSQL
# DATABASES = { ... }
```

**No need to reinstall** - SQLite comes with Python!

---

## 📁 Project Structure (Development)

```
d:\Huy\Project\programing\website-bmc\
│
├── venv/                           # Virtual environment (created)
│   ├── Scripts/
│   │   ├── activate.bat           # Activation script
│   │   ├── python.exe             # Python interpreter
│   │   └── ...
│   └── Lib/
│       └── site-packages/         # Installed packages
│
├── BMC/                            # Django project config
│   ├── settings.py                # ✅ UPDATED for Windows
│   ├── urls.py
│   ├── wsgi.py
│   └── asgi.py
│
├── BmcBase/                        # Main app
│   ├── models.py
│   ├── views.py
│   ├── urls.py
│   └── templates/
│
├── legal/                          # Legal pages app
│   ├── views.py
│   ├── urls.py
│   └── templates/
│
├── db.sqlite3                      # ✅ SQLite database (auto-created)
├── staticfiles/                    # Collected static files
├── media/                          # User uploads
│   ├── products/
│   ├── banners/
│   ├── contacts/
│   └── uploads/
├── locale/                         # i18n translations
│
├── WINDOWS_DEV_SETUP.md           # ✅ NEW - Setup guide
├── WINDOWS_CONFIG_SUMMARY.md      # ✅ NEW - This file
├── requirements.txt               # ✅ NEW - Dependencies
├── .env.example                   # ✅ NEW - Environment template
├── run_dev.bat                    # ✅ NEW - Windows batch launcher
├── run_dev.ps1                    # ✅ NEW - PowerShell launcher
│
├── manage.py                       # Django CLI
└── .gitignore                      # Git ignore file
```

---

## 🔐 Security Settings

### Development (Current)
```python
DEBUG = True                          # All errors shown
SECRET_KEY = 'hard-coded'            # In settings.py
DATABASE = 'sqlite'                  # Local file
ALLOWED_HOSTS = ['localhost', ...]   # Any local address
```

### Production (Should Be)
```python
DEBUG = False                         # No error details
SECRET_KEY = os.getenv('SECRET_KEY') # From .env file
DATABASE = 'postgresql'               # Secure server
ALLOWED_HOSTS = ['bmcgroup.com.vn']  # Domain only
SECURE_SSL_REDIRECT = True            # HTTPS only
```

⚠️ **Never use development settings in production!**

---

## ✅ Verification Checklist

After setup, verify everything:

- [ ] Virtual environment created and activated
- [ ] Dependencies installed (`pip list` shows Django, etc.)
- [ ] Database created (db.sqlite3 exists)
- [ ] Migrations applied (no pending migrations)
- [ ] Static files collected (staticfiles/ has content)
- [ ] Admin user created (can login to /admin/)
- [ ] Server runs without errors (`python manage.py runserver`)
- [ ] Homepage loads (`http://127.0.0.1:8000/`)
- [ ] Admin accessible (`http://127.0.0.1:8000/admin/`)
- [ ] Legal pages work (`http://127.0.0.1:8000/legal/privacy/`)

---

## 🐛 Common Issues & Fixes

### Issue: "ModuleNotFoundError: No module named 'django'"
**Fix**: Activate virtual environment and install:
```bash
venv\Scripts\activate
pip install -r requirements.txt
```

### Issue: "django.db.utils.OperationalError"
**Fix**: Run migrations:
```bash
python manage.py migrate
```

### Issue: Port 8000 already in use
**Fix**: Use different port:
```bash
python manage.py runserver 8001
```

### Issue: Static files not loading
**Fix**: Collect static files:
```bash
python manage.py collectstatic --noinput
```

### Issue: db.sqlite3 file permissions error
**Fix**: Delete and recreate:
```bash
del db.sqlite3
python manage.py migrate
```

See **WINDOWS_DEV_SETUP.md** for more troubleshooting.

---

## 📚 File Reference

| File | Purpose | Status |
|------|---------|--------|
| BMC/settings.py | Django configuration | ✅ Updated |
| WINDOWS_DEV_SETUP.md | Setup guide | ✅ New |
| WINDOWS_CONFIG_SUMMARY.md | This summary | ✅ New |
| requirements.txt | Dependencies | ✅ New |
| .env.example | Environment template | ✅ New |
| run_dev.bat | Windows launcher | ✅ New |
| run_dev.ps1 | PowerShell launcher | ✅ New |
| db.sqlite3 | SQLite database | ✅ Will be created |

---

## 🎉 You're Ready!

Everything is configured for Windows development!

**Next steps**:
1. Create virtual environment: `python -m venv venv`
2. Activate it: `venv\Scripts\activate`
3. Install dependencies: `pip install -r requirements.txt`
4. Run migrations: `python manage.py migrate`
5. Start server: `python manage.py runserver`
6. Open: http://127.0.0.1:8000/

**Or just run**: `run_dev.bat` for an interactive menu!

---

## 📞 Help & Resources

- **Setup Guide**: See WINDOWS_DEV_SETUP.md
- **Django Docs**: https://docs.djangoproject.com/
- **Python Docs**: https://docs.python.org/
- **SQLite Docs**: https://www.sqlite.org/

---

**Status**: ✅ Windows Development Environment Configured  
**Date**: April 18, 2026  
**Ready to Use**: YES 🚀
