按照Tom的django-tenants教程操作两次仍出现404错误求助
Let’s walk through the most likely causes for your 404 errors—since you’ve followed the tutorial twice, this is almost certainly a small configuration or setup detail you might have missed. Here’s how to diagnose and fix each issue:
1. Verify Tenant & Domain Records Exist and Are Linked
First, make sure your t1 tenant and its associated domain are properly created in the database. This is the #1 cause of tenant-specific 404s.
Open a Django shell (python manage.py shell) and run these checks:
from customers.models import Client, Domain # Check if the t1 tenant exists try: tenant = Client.objects.get(schema_name='t1') print(f"Tenant found: {tenant.name} (schema: {tenant.schema_name})") except Client.DoesNotExist: print("ERROR: t1 tenant does not exist! You need to create it first.") # Check if the domain t1.dtdemo.local is linked to this tenant domain_exists = Domain.objects.filter(tenant=tenant, domain='t1.dtdemo.local', is_primary=True).exists() print(f"Domain t1.dtdemo.local linked to tenant: {domain_exists}")
- If the tenant doesn’t exist, re-run the tenant creation steps from the tutorial (e.g., using a management command or admin interface).
- If the domain is missing, create it with
is_primary=Truefor the t1 tenant.
2. Validate URL Configuration Structure
Django-tenants requires splitting your URLs into public schema routes and tenant-specific routes. Your root urls.py (dtdemo2.urls) needs to handle this correctly.
Make sure your root urls.py looks something like this:
from django.contrib import admin from django.urls import path, include from django_tenants.views import home as public_home # Public schema routes (for non-tenant domains like dtdemo.local) urlpatterns = [ path('', public_home, name='public-home'), path('admin/', admin.site.urls), # This manages tenants, not tenant-specific content ] # Tenant-specific routes (loaded when a tenant domain is accessed) tenant_urlpatterns = [ path('', include('myclientapp.urls')), # Your tenant app's homepage path('admin/', admin.site.urls), # Tenant-specific admin interface ]
Also, confirm your myclientapp has a urls.py with a route for the homepage (e.g., mapping '' to a view). If this file is missing or empty, you’ll get a 404 for the tenant homepage.
3. Ensure Tenant Schemas Have Run Migrations
Even if you ran migrate for the public schema, you need to run migrations specifically for tenant schemas. Run this command to apply migrations to the t1 schema:
python manage.py tenant_command migrate --schema=t1
This creates all the necessary tables (including admin tables) in the tenant’s schema. Without this, the tenant admin will throw a 404 because the required database tables don’t exist.
4. Check Database Schema Exists
Since you’re using PostgreSQL, verify that the t1 schema exists in your database. Run this in a psql terminal:
SELECT schema_name FROM information_schema.schemata WHERE schema_name = 't1';
If no results are returned, the schema wasn’t created when you made the tenant. Recreate the tenant to generate the schema, or manually create it (not recommended—better to use the django-tenants model methods).
5. Confirm Middleware & App Configurations
Your settings look mostly correct, but double-check these details:
django_tenants.middleware.main.TenantMainMiddlewareis the first middleware in yourMIDDLEWARElist (you already have this right—great!).django.contrib.sitesis only inSHARED_APPS(you have this correct too—don’t add it toTENANT_APPS).myclientappis inTENANT_APPS(it is, so that’s good).
6. Check Logs for Tenant Context Clues
You’ve set up tenant logging, so watch the console when you make a request to t1.dtdemo.local:8000. Look for lines like:
[t1:t1.dtdemo.local] DEBUG 2024-05-20 12:34:56 Some log message
- If the schema name shows
publicinstead oft1, that means the domain wasn’t matched to a tenant—go back to step 1 to fix your Domain record. - If it shows
t1but you still get a 404, the issue is likely in your URL routing (step 2).
Final Quick Checks
- Double-check your
hostsfile: you have127.0.0.1 t1.dtdemo.localadded, which is correct—make sure you’re not accidentally accessingt1.dtdemo2.localinstead. - Clear your browser cache or use incognito mode to rule out cached redirects or cookies.
内容的提问来源于stack exchange,提问作者Karl Hoffmann

