Django 1.9无法导入django.urls,更换DRF版本无法使用path对象求助
Got it, let's fix this for Django 1.9! Since Django 1.9 doesn't support path() from django.urls, we need to stick with the older url() function (which uses regex patterns) while keeping your existing structure intact. Here's the adjusted code:
from django.contrib import admin from django.conf.urls import include, url from rest_slack.views import DRSEventView, DRSCommandView # Use url() instead of path() since Django 1.9 doesn't support path api_patterns = ([ url(r'^web/$', DRSEventView.as_view()), url(r'^events/$', DRSCommandView.as_view()), ], 'api') urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^api/v1/', include(api_patterns)), ]
Key adjustments made:
- Removed the
from django.urls import pathimport entirely — Django 1.9 doesn't include this module, so it was causing your import error. - Replaced each
path()call withurl()using regex patterns that mirror the exact matching behavior ofpath():path('web/', ...)translates tourl(r'^web/$', ...)— the^and$ensure it's an exact match (just like the default behavior ofpath()in newer Django versions)- Same logic applies to
path('events/', ...)→url(r'^events/$', ...)
- Kept your
api_patternstuple with the namespace ('api') intact —include()works the same way with regex-based URL patterns in Django 1.9, so your namespace setup stays functional.
This setup will work perfectly with Django 1.9 without needing to switch DRF versions, since we're using the routing syntax native to your Django version.
内容的提问来源于stack exchange,提问作者Srikant Singh
相关产品推荐
相关产品推荐

