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

Django 1.11中rest_framework_docs导入patterns报错求解决

Hey there, let's fix this issue right away. The error you're seeing is exactly what you suspected—your installed version of rest_framework_docs is relying on Django's deprecated patterns module, which was phased out starting in Django 1.10 and fully removed in later versions. Here are two straightforward solutions:

Solution 1: Patch the rest_framework_docs source code (quick fix)

Since the problem is isolated to the library's urls.py file, you can modify it directly to work with Django 1.11:

  1. Locate the urls.py file of the installed library. From your error log, it's at:
    /usr/local/lib/python3.5/dist-packages/rest_framework_docs/urls.py
    
  2. Open this file in a text editor and make two changes:
    • Update the import line: Remove patterns from the import statement. Change:
      from django.conf.urls import patterns, include, url
      
      to:
      from django.conf.urls import include, url
      
    • Replace patterns() with a list: Find the line where urlpatterns is defined. It will look something like this:
      urlpatterns = patterns('',
          url(r'^$', docs_view, name='docs_index'),
          # Other URL patterns here
      )
      
      Rewrite it to use a standard Python list (Django 1.11 supports this natively):
      urlpatterns = [
          url(r'^$', docs_view, name='docs_index'),
          # Other URL patterns here
      ]
      
  3. Save the file and restart your Django server. The error should be gone.

Solution 2: Switch to a maintained DRF documentation library (long-term fix)

rest_framework_docs hasn't been updated in years, so you'll likely run into more compatibility issues down the line. For a more robust solution, switch to one of these actively maintained libraries that work seamlessly with Django 1.11 and Python 3:

Option A: drf-yasg (OpenAPI 2.0 support)

  1. Install the package:
    pip install drf-yasg
    
  2. Add it to your INSTALLED_APPS in settings.py:
    INSTALLED_APPS = [
        # Your existing apps
        'drf_yasg',
    ]
    
  3. Update your project's urls.py to add the documentation endpoints:
    from rest_framework import permissions
    from drf_yasg.views import get_schema_view
    from drf_yasg import openapi
    
    schema_view = get_schema_view(
        openapi.Info(
            title="Your API Documentation",
            default_version='v1',
            description="Comprehensive docs for your API endpoints",
            terms_of_service="https://your-domain.com/terms/",
            contact=openapi.Contact(email="your-email@example.com"),
            license=openapi.License(name="BSD License"),
        ),
        public=True,
        permission_classes=(permissions.AllowAny,),
    )
    
    urlpatterns = [
        # Your existing URL patterns
        url(r'^docs/swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
        url(r'^docs/redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
    ]
    

Option B: drf-spectacular (OpenAPI 3.0+ support)

If you need more advanced features (like request/response validation, webhook support), go with drf-spectacular. It's compatible with Django 1.11 and offers a more modern documentation experience.

Once you set up either of these libraries, remove the old rest_framework_docs import and URL pattern from your project.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:19:19