You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用django-extensions的shell_plus时出现ModuleNotFoundError求助

Fixing "ModuleNotFoundError: No module named 'django.contrib.admin.templatetags.admin_static'" with shell_plus

Let's break down what's happening and how to fix this quickly:

Why This Error Occurs

Django 3.0 removed the deprecated django.contrib.admin.templatetags.admin_static module — it was replaced by the standard static templatetag from django.templatetags.static. However, the version of django-extensions you're using (2.2.1) still tries to import this removed module when launching shell_plus, which triggers the error.

This is the cleanest and most maintainable solution. Versions of django-extensions 2.2.5 and newer are fully compatible with Django 3.0, and they've fixed the broken reference to admin_static.

Run this command to upgrade:

pip install --upgrade django-extensions>=2.2.5

Once the upgrade finishes, start shell_plus again — it should load all your models automatically without errors:

python manage.py shell_plus

Temporary Workaround (If You Can't Upgrade)

If you're stuck on django-extensions==2.2.1 for specific reasons, you can create a dummy module to resolve the missing import:

  1. Create this directory structure in your project root (make sure each folder contains an empty __init__.py file to mark it as a Python package):
    django/contrib/admin/templatetags/
    
  2. Inside that folder, create a file named admin_static.py with this code:
    from django.templatetags.static import static
    from django.utils.deprecation import RemovedInDjango40Warning
    import warnings
    
    warnings.warn(
        "admin_static is deprecated; use django.templatetags.static instead",
        RemovedInDjango40Warning,
    )
    
    __all__ = ['static']
    

This redirects the old admin_static import to the supported static templatetag, and adds a warning to remind you to upgrade when possible.

Verify the Fix

After applying either solution, run python manage.py shell_plus again. It should now work as expected, with all your models pre-loaded into the shell.

内容的提问来源于stack exchange,提问作者codyc4321

火山引擎 最新活动