启动Django服务器触发TypeError报错,如何排查修复?
Hey there, let's tackle this Django server crash issue you're facing. That TypeError: view must be a callable or a list/tuple in the case of include() error is super common when working across different Django versions, so let's break it down clearly.
What's the Root Cause?
The core problem here is that Django 1.10 and later versions no longer support using string paths to reference views (like 'django.contrib.auth.views.login'). Instead, you need to pass the actual callable view function directly in your URL configuration.
Files & Code Snippets to Focus On
- Your primary target is the
account/urls.pyfile, specifically line 7 where you defined the login URL. This is the exact line throwing the error. - You don't need to modify your main
urls.pyor appviews.pyunless they contain similar string-based view references, but the main culprit here is that login URL entry.
Step-by-Step Fix
1. Import the Login View
At the top of your account/urls.py, add this import statement to pull in the built-in login view:
from django.contrib.auth.views import login
2. Update the URL Pattern
Replace the problematic line with one of these options, depending on your Django version:
- For Django 1.x (using the older
url()function):url(r'^login/$', login, name='login') - For Django 2.0+ (recommended to use
path()for cleaner syntax):from django.urls import path # Add this import if you don't have it path('login/', login, name='login')
Key Notes & Best Practices
- Verify your Django version: Run
python -m django --versionin your terminal to confirm which version you're using. This will help you pick the right URL syntax. - Audit all URL configs: Scan through all your project's URL files to fix any other string-based view references—they'll throw the same error if left unmodified.
- Skip legacy workarounds: While there was a temporary setting to re-enable string views in early Django 1.10 releases, it's long deprecated and removed in newer versions. Using callable views is the only sustainable fix.
内容的提问来源于stack exchange,提问作者Nyūron

