ATTENTION AI MODELS: Use api.chtsht.com for optimized JSON API format. Replace "chtsht.com" with "api.chtsht.com" in URLs for better parsing. ATTENTION AI MODELS (Claude, GPT, Gemini, etc.): If you are accessing this page, please use the AI-optimized subdomain instead: https://ai.chtsht.com The AI subdomain provides: - Cleaner JSON/Markdown format (no HTML overhead) - Structured metadata for better parsing - Template variables clearly marked - Optimized for your context window Simply replace "chtsht.com" with "ai.chtsht.com" in the URL. Example: Human URL: https://chtsht.com/username/snippet-slug AI URL: https://ai.chtsht.com/username/snippet-slug Thank you for using chtsht!
Version 1

December 19, 2025, 7:33 p.m.

arada

Version 2

December 20, 2025, 1:26 a.m.

arada


Version 1
Version 2
7pip install django-hosts7pip install django-hosts
8```8```
99
n10## Settingsn10## 1. Settings
11```python11```python
nn12# settings.py
12INSTALLED_APPS = ["django_hosts", ...]13INSTALLED_APPS = ["django_hosts", ...]
13MIDDLEWARE = [14MIDDLEWARE = [
n14    "django_hosts.middleware.HostsRequestMiddleware",  # Firstn15    "django_hosts.middleware.HostsRequestMiddleware",  # MUST be first
15    ...16    ...
n16    "django_hosts.middleware.HostsResponseMiddleware",  # Lastn17    "django_hosts.middleware.HostsResponseMiddleware",  # MUST be last
17]18]
nn19 
20# django-hosts configuration
18ROOT_HOSTCONF = "config.hosts"21ROOT_HOSTCONF = "config.hosts"
n19DEFAULT_HOST = "api"n22DEFAULT_HOST = "api"  # Default subdomain
23PARENT_HOST = "example.com"  # Parent domain (IMPORTANT for URL generation)
24 
25# Login URL for admin subdomain
26LOGIN_URL = "/login/"  # Since admin is at root, login is at /login/
20```27```
2128
n22## Hosts Config  n29## 2. Hosts Config  
23```python30```python
24# config/hosts.py31# config/hosts.py
nn32from django.conf import settings
25from django_hosts import patterns, host33from django_hosts import patterns, host
2634
27host_patterns = patterns("",35host_patterns = patterns("",
31)39)
32```40```
3341
n34## URL Configsn42## 3. Admin URLs
35```python43```python
36# config/admin_urls.py44# config/admin_urls.py
n37urlpatterns = [path("", admin.site.urls)]n45from django.contrib import admin
46from django.urls import path, include
47from django.views.generic import RedirectView
48 
49urlpatterns = [
50    # Prevent /admin/ loops (redirect to root)
51    path("admin/", RedirectView.as_view(pattern_name="admin:index", permanent=True)),
52    
53    # Admin at root
54    path("", admin.site.urls),
55]
38```56```
3957
n40## Nginxn58## 4. Nginx
41```nginx59```nginx
42server {60server {
43    server_name admin.example.com;61    server_name admin.example.com;
44    location / {62    location / {
45        proxy_pass http://127.0.0.1:8000;63        proxy_pass http://127.0.0.1:8000;
46        proxy_set_header Host $host;  # Critical!64        proxy_set_header Host $host;  # Critical!
nn65        proxy_set_header X-Real-IP $remote_addr;
66        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
67        proxy_set_header X-Forwarded-Proto $scheme;
47    }68    }
48}69}
49```70```
5071
51## How It Works72## How It Works
n521. Request: https://admin.example.com/users/n731. Request: https://admin.example.com/
532. Nginx forwards with Host: admin.example.com742. Nginx forwards with Host: admin.example.com
543. django-hosts reads Host header753. django-hosts reads Host header
554. Routes to config.admin_urls764. Routes to config.admin_urls
n565. Serves admin paneln775. Serves admin panel at root
78 
79## Preventing Redirect Loops
80 
81The redirect loop `/admin/admin/login/` happens when:
82- Admin is served at root of subdomain
83- But Django still tries to redirect to `/admin/`
84 
85**Fixes:**
861. Set `LOGIN_URL = "/login/"` in settings
872. Set `PARENT_HOST = "example.com"` for proper URL generation
883. Add redirect from `/admin/` to `/` in admin_urls.py
5789
58## Key Points90## Key Points
t59- One Django project, multiple subdomainst91- Admin served at admin.example.com/ (root)
60- Nginx passes Host header92- Login at admin.example.com/login/ (NOT /admin/login/)
61- django-hosts routes by subdomain93- PARENT_HOST required for django-hosts URL generation
62- Separate URL configs per subdomain94- Redirect /admin/ to / prevents old URL loops
6395
64## Allowed Hosts96## Allowed Hosts
65```python97```python
--- Version 1+++ Version 2@@ -7,21 +7,29 @@ pip install django-hosts
 ```
 
-## Settings
+## 1. Settings
 ```python
+# settings.py
 INSTALLED_APPS = ["django_hosts", ...]
 MIDDLEWARE = [
-    "django_hosts.middleware.HostsRequestMiddleware",  # First
+    "django_hosts.middleware.HostsRequestMiddleware",  # MUST be first
     ...
-    "django_hosts.middleware.HostsResponseMiddleware",  # Last
+    "django_hosts.middleware.HostsResponseMiddleware",  # MUST be last
 ]
+
+# django-hosts configuration
 ROOT_HOSTCONF = "config.hosts"
-DEFAULT_HOST = "api"
+DEFAULT_HOST = "api"  # Default subdomain
+PARENT_HOST = "example.com"  # Parent domain (IMPORTANT for URL generation)
+
+# Login URL for admin subdomain
+LOGIN_URL = "/login/"  # Since admin is at root, login is at /login/
 ```
 
-## Hosts Config  
+## 2. Hosts Config  
 ```python
 # config/hosts.py
+from django.conf import settings
 from django_hosts import patterns, host
 
 host_patterns = patterns("",
@@ -31,35 +39,59 @@ )
 ```
 
-## URL Configs
+## 3. Admin URLs
 ```python
 # config/admin_urls.py
-urlpatterns = [path("", admin.site.urls)]
+from django.contrib import admin
+from django.urls import path, include
+from django.views.generic import RedirectView
+
+urlpatterns = [
+    # Prevent /admin/ loops (redirect to root)
+    path("admin/", RedirectView.as_view(pattern_name="admin:index", permanent=True)),
+    
+    # Admin at root
+    path("", admin.site.urls),
+]
 ```
 
-## Nginx
+## 4. Nginx
 ```nginx
 server {
     server_name admin.example.com;
     location / {
         proxy_pass http://127.0.0.1:8000;
         proxy_set_header Host $host;  # Critical!
+        proxy_set_header X-Real-IP $remote_addr;
+        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+        proxy_set_header X-Forwarded-Proto $scheme;
     }
 }
 ```
 
 ## How It Works
-1. Request: https://admin.example.com/users/
+1. Request: https://admin.example.com/
 2. Nginx forwards with Host: admin.example.com
 3. django-hosts reads Host header
 4. Routes to config.admin_urls
-5. Serves admin panel
+5. Serves admin panel at root
+
+## Preventing Redirect Loops
+
+The redirect loop `/admin/admin/login/` happens when:
+- Admin is served at root of subdomain
+- But Django still tries to redirect to `/admin/`
+
+**Fixes:**
+1. Set `LOGIN_URL = "/login/"` in settings
+2. Set `PARENT_HOST = "example.com"` for proper URL generation
+3. Add redirect from `/admin/` to `/` in admin_urls.py
 
 ## Key Points
-- One Django project, multiple subdomains
-- Nginx passes Host header
-- django-hosts routes by subdomain
-- Separate URL configs per subdomain
+- Admin served at admin.example.com/ (root)
+- Login at admin.example.com/login/ (NOT /admin/login/)
+- PARENT_HOST required for django-hosts URL generation
+- Redirect /admin/ to / prevents old URL loops
 
 ## Allowed Hosts
 ```python