[Fixed]-Unsupported operand type(s) for +: 'RegexURLPattern' and 'str'

1👍

You’re confusing the two available syntaxes.

The patterns() function takes a prefix as the first argument, which is a (possibly empty) string, and returns a list with the prefix added to each pattern. The other arguments are url() instances. A plain list should only contain url() instances, and you can’t add a common prefix.

patterns() is deprecated, so it’s better to use a list everywhere:

accounts/urls.py

urlpatterns = [
   url(r'^register$', 'accounts.views.register', name='register'),
   url(r'^login$', 'accounts.views.login', name='login'),
   url(r'^logout$', 'accounts.views.logout', name='logout'),
]

urls.py

urlpatterns = [
    url(r'^accounts/', include('accounts.urls', namespace='accounts')),
    url(r'^admin/', include(admin.site.urls)),
]

As for importing views: you need to import them if you pass the function itself. If you don’t import it first, the function is not available and results in a NameError. Right now you’re passing an import path, not the function, so you don’t have to import anything. This is deprecated as well (for views, not for including other url configurations), so for compatibility with future Django versions you can change it to this:

from django.conf.urls import url
from accounts import views

urlpatterns = [
   url(r'^register$', views.register, name='register'),
   url(r'^login$', views.login, name='login'),
   url(r'^logout$', views.logout, name='logout'),
]

Note that the second argument to url() is no longer a string, but the view function itself in the imported views module.

👤knbk

0👍

Edit your app urls.py to this:

from django.conf.urls import patterns, url
from accounts import views

urlpatterns = patterns('', // add the empty string here
   url(r'^register$', 'accounts.views.register', name='register'),
   url(r'^login$', 'accounts.views.login', name='login'),
   url(r'^logout$', 'accounts.views.logout', name='logout'),
)

Also, remove the empty string from your main urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = [
    url(r'^accounts/', include('accounts.urls', namespace='accounts')),
    url(r'^admin/', include(admin.site.urls)),
  ]

because I donèt think it is needed when using the square brackets version.

Leave a comment