Django urls dispatcher error(urls.E004)确保urlpatterns是url()实例列表

时间:2016-12-08 22:58:14

标签: python django django-urls

我试图将国际化添加到我的Django项目中。并且我将i18n_patterns()添加到我的主urls.py我有以下问题:

ERRORS:
    ?: (urls.E004) Your URL pattern [<RegexURLResolver <module 'coffeebar.urls' from './coffee/coffeebar/urls.py'> (coffeebar:coffeebar) ^bar/>, <RegexURLResolver <RegexURLPattern list> (admin:admin) ^admin/>] is invalid. Ensure that urlpatterns is a list of url() instances.

./ coffee / coffee / urls.py (主网址配置)

from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin


urlpatterns = i18n_patterns([
    url(r'^bar/', include('coffeebar.urls')),
    url(r'^admin/', admin.site.urls),
])

./ coffee / coffeebar / urls.py (包含网址配置)

from django.conf.urls import url, include
from django.contrib.auth import views as auth_views

from . import views

app_name = 'coffeebar'

order = [
    url(r'^$', views.order_details, name='index'),
    url(r'^(?P<order_id>[0-9]+)/$', views.order_details, name='details'),

    url(r'^add/$', views.order_add_item, name='add'),
    url(r'^remove/$', views.order_remove_item, name='remove'),
    url(r'^checkout/$', views.order_checkout, name='checkout'),
    url(r'^list/$', views.order_list, name='list'),
]

admin = [
    url(r'^$', views.admin, name='index'),
    url(r'^accounts/', include([
        url(r'^$', views.admin_accounts, name='index'),
        url(r'^open/$', views.admin_accounts, {'action': 'open'}, name='open'),
        url(r'^close/$', views.admin_accounts, {'action': 'close'}, name='close'),
    ], namespace='accounts')),

    url(r'^orders/', include([
        url(r'^$', views.admin_orders, name='index'),
        url(r'^update/', views.admin_orders, {'action': 'update'}, name='update'),
    ], namespace='orders')),

    url(r'^products/', include([
        url(r'^$', views.admin_products, name='index'),
        url(r'^add/$', views.admin_products, {'action': 'add'}, name='add'),
        url(r'^(?P<product_id>[0-9]+)/', views.admin_products, {'action': 'edit'}, name='edit'),
        url(r'^(?P<product_id>[0-9]+)/toggle/$', views.admin_products, {'action': 'toggle'}, name='toggle'),
        url(r'^(?P<product_id>[0-9]+)/delete/$', views.admin_products, {'action': 'delete'}, name='delete'),
    ], namespace='products')),
]

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'order/', include(order, namespace='order')),
    url(r'admin/', include(admin, namespace='admin')),

    # django auth
    url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
    url(r'^logout/$', auth_views.logout, {'template_name': 'logout.html'}, name='logout'),
]

1 个答案:

答案 0 :(得分:4)

删除括号,更改:

urlpatterns = i18n_patterns([
    url(r'^bar/', include('coffeebar.urls')),
    url(r'^admin/', admin.site.urls),
])

为:

urlpatterns = i18n_patterns(
        url(r'^bar/', include('coffeebar.urls')),
        url(r'^admin/', admin.site.urls),
    )