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:
- Locate the
urls.pyfile of the installed library. From your error log, it's at:/usr/local/lib/python3.5/dist-packages/rest_framework_docs/urls.py - Open this file in a text editor and make two changes:
- Update the import line: Remove
patternsfrom the import statement. Change:
to:from django.conf.urls import patterns, include, urlfrom django.conf.urls import include, url - Replace
patterns()with a list: Find the line whereurlpatternsis defined. It will look something like this:
Rewrite it to use a standard Python list (Django 1.11 supports this natively):urlpatterns = patterns('', url(r'^$', docs_view, name='docs_index'), # Other URL patterns here )urlpatterns = [ url(r'^$', docs_view, name='docs_index'), # Other URL patterns here ]
- Update the import line: Remove
- 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)
- Install the package:
pip install drf-yasg - Add it to your
INSTALLED_APPSinsettings.py:INSTALLED_APPS = [ # Your existing apps 'drf_yasg', ] - Update your project's
urls.pyto 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

