> ## Documentation Index
> Fetch the complete documentation index at: https://help.noxity.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy Django

> Run a Django app on the Python Selector. Wire up passenger_wsgi.py, run migrations, serve static files.

Django apps run on the Python Selector via a thin `passenger_wsgi.py` shim. Static files are served by Apache, not Django.

For the underlying tool, see [Python](/web-hosting/virtual-containers/python).

## Before you begin

* A Python app created in cPanel, version 3.10 or 3.11.
* The app root contains your Django project (`manage.py`, `<projectname>/settings.py`).
* A `requirements.txt` listing Django and your other packages.

## Wire up Passenger

<Steps>
  <Step title="Create passenger_wsgi.py in the app root">
    ```python passenger_wsgi.py theme={}
    import os
    import sys

    sys.path.insert(0, '/home/<user>/myapp')

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')

    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    ```

    Replace `<user>` and `myapp` with your real cPanel user and project name.
  </Step>

  <Step title="Install dependencies">
    Click **Run Pip Install** in the cPanel app management page. Make sure `requirements.txt` is listed under **Configuration files**.
  </Step>

  <Step title="Run migrations and collect static files">
    Over SSH:

    ```bash theme={}
    source ~/virtualenv/myapp/3.11/bin/activate && cd ~/myapp
    python manage.py migrate
    python manage.py collectstatic --noinput
    ```
  </Step>

  <Step title="Restart">
    Click **Restart** in the cPanel UI.
  </Step>
</Steps>

## Serve static files

Django doesn't serve `/static/` in production. Have Apache do it. Add to the app root's `.htaccess` above the Selector-managed block:

```apache theme={}
Alias /static/ /home/<user>/myapp/staticfiles/
<Directory "/home/<user>/myapp/staticfiles">
  Require all granted
</Directory>
```

Set `STATIC_ROOT = BASE_DIR / 'staticfiles'` in `settings.py` so `collectstatic` writes there.

## Common issues

<AccordionGroup>
  <Accordion title="500 after the first request, log shows ImproperlyConfigured">
    `DJANGO_SETTINGS_MODULE` doesn't match your project layout. Open `passenger_wsgi.py` and confirm the module path. Then `touch tmp/restart.txt`.
  </Accordion>

  <Accordion title="`pip install` fails with a memory error on a heavy package">
    Common with `numpy`, `pandas`, `Pillow`. Install one at a time over SSH, or pre-build wheels on a dev machine.
  </Accordion>

  <Accordion title="ALLOWED_HOSTS error">
    Add your domain to `ALLOWED_HOSTS` in `settings.py`. Restart.
  </Accordion>
</AccordionGroup>

## Need a hand?

<CardGroup cols={2}>
  <Card title="Open a ticket" icon="life-ring" href="https://members.noxity.io/submitticket.php">
    Best for anything that needs an account check or a config change on our end.
  </Card>

  <Card title="Live chat" icon="messages" href="https://noxity.io/contact">
    Faster for quick questions during business hours.
  </Card>
</CardGroup>
