You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

启动Django服务器触发TypeError报错,如何排查修复?

Fixing Django's "view must be a callable" TypeError on Server Start

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.py file, 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.py or app views.py unless 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 --version in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:07:41