# Media Serving Fix Instructions

## Issue
Images are returning 404 errors like:
```
Page not found (404)
"/home/acbptxvs/public_html/branchbusinessadvance.co.ke/media/kyc/id_documents/FRONT-ID.jpeg" does not exist
Request URL: https://branchbusinessadvance.co.ke/media/kyc/id_documents/FRONT-ID.jpeg
Raised by: django.views.static.serve
```

## Root Cause
The issue is that Django is trying to serve media files through `django.views.static.serve` instead of letting the web server (Apache) handle them directly. This causes incorrect path resolution in production.

## Solution
Fix the URL configuration to let the web server serve media files directly.

## Steps

### 1. Upload the fix script
Upload `fix_media_urls.py` to your cPanel file manager in the same directory as your Django project.

### 2. Run the fix script
In cPanel's "Execute Python Script" feature, run:

```python
python fix_media_urls.py
```

### 3. Restart your Django application
**CRITICAL STEP:** After the script completes:
1. Go to cPanel → Python App
2. Find your HAVEN GRAZURI INVESTMENT LIMITEDapplication
3. Click "Restart"
4. Wait for the restart to complete

### 4. Test the images
After restart:
1. Go to your application
2. View user profiles with images
3. Check that images now load properly

## What the script does

1. ✅ **Backs up current URLs configuration**
2. ✅ **Updates `branch_system/urls.py`** to:
   - Remove Django media serving in production
   - Keep Django serving for development
   - Let Apache handle media files directly
3. ✅ **Creates optimized `.htaccess`** for media directory with:
   - Proper MIME types
   - Compression settings
   - Cache headers
   - Security settings
4. ✅ **Verifies media directory structure**

## Expected output

You should see:
```
🎉 MEDIA URL FIX COMPLETED SUCCESSFULLY!
✅ URLs configuration updated
✅ Media .htaccess created
✅ Media directory structure verified

🔄 IMPORTANT: Restart your Django application!
```

## Before vs After

**Before (causing 404s):**
- Django tries to serve: `/home/acbptxvs/public_html/branchbusinessadvance.co.ke/media/kyc/id_documents/FRONT-ID.jpeg`
- Uses `django.views.static.serve`
- Incorrect absolute path resolution

**After (working):**
- Apache serves directly: `https://branchbusinessadvance.co.ke/media/kyc/id_documents/FRONT-ID.jpeg`
- No Django involvement
- Correct web server handling

## Alternative: Run updated setup.py

You can also run the updated `setup.py` which includes this fix:

```python
python setup.py
```

This will run the complete setup process including the media URL fixes.

## Verification

After the fix and restart:
1. ✅ Images should load without 404 errors
2. ✅ Browser developer tools should show 200 OK for image requests
3. ✅ Both old and new user images should work
4. ✅ No more `django.views.static.serve` in error messages

## If images still don't load

1. **Check restart**: Ensure you restarted the Django application
2. **Check file permissions**: Verify media directory is readable by web server
3. **Check file paths**: Run `fix_production_image_paths.py` to fix database paths
4. **Check .htaccess**: Ensure media/.htaccess was created properly
5. **Check browser cache**: Clear browser cache and try again

## Technical Details

The fix changes the URL configuration from:
```python
# OLD (problematic)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
```

To:
```python
# NEW (working)
if settings.DEBUG:
    # Development only
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
else:
    # Production: let web server handle media files
    pass
```

This ensures media files are served efficiently by Apache in production while maintaining Django serving for development.