Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ensure that urlpatterns is a list of path() and/or re_path() instances

What's wrong with my urlpatterns?

urlpatterns = [
    re_path(r'^dj-admin/', admin.site.urls),
    re_path(r'^admin/', include(wagtailadmin_urls)),
    re_path(r'^docs/', include(wagtaildocs_urls)),

    i18n_patterns(
        path(r'', include(wagtail_urls)),
        prefix_default_language = False
    )
]

ERRORS:
?: (urls.E004) Your URL pattern [ (None:None) ''>] is invalid. Ensure that urlpatterns is a list of path() and/or re_path() instances.

To my best understanding it is equivalent to the example in the docs:

urlpatterns = [
    path('sitemap.xml', sitemap, name='sitemap-xml'),
]

urlpatterns += i18n_patterns(
    path('about/', about_views.main, name='about'),
)

BTW:

In [1]: import django

In [2]: django.__version__
Out[2]: '2.0.5'
like image 791
NarūnasK Avatar asked Jan 29 '23 03:01

NarūnasK


1 Answers

You have put i18n_patterns inside a patterns list, but this function itself produces a list of urlpatterns, not a single pattern. This is not equivalent to the documentation you found.

Use concatenation:

urlpatterns = [
    re_path(r'^dj-admin/', admin.site.urls),
    re_path(r'^admin/', include(wagtailadmin_urls)),
    re_path(r'^docs/', include(wagtaildocs_urls)),
] + i18n_patterns(
    path(r'', include(wagtail_urls)),
    prefix_default_language = False
)

or prefix the function call with * to incorporate all elements into the list:

urlpatterns = [
    re_path(r'^dj-admin/', admin.site.urls),
    re_path(r'^admin/', include(wagtailadmin_urls)),
    re_path(r'^docs/', include(wagtaildocs_urls)),

    *i18n_patterns(
        path(r'', include(wagtail_urls)),
        prefix_default_language = False
    )
]

This is called iterable unpacking and requires Python 3.5 or newer.

The documentation used += augmented assignment to extend the urlpatterns list, which is probably a good pattern for you to just re-use:

urlpatterns = [
    re_path(r'^dj-admin/', admin.site.urls),
    re_path(r'^admin/', include(wagtailadmin_urls)),
    re_path(r'^docs/', include(wagtaildocs_urls)),
]

urlpatterns += i18n_patterns(
    path(r'', include(wagtail_urls)),
    prefix_default_language = False
)
like image 148
Martijn Pieters Avatar answered Jan 31 '23 10:01

Martijn Pieters