[Django]-TypeError at / 'str' object is not a mapping in django template

54👍

Check that you have properly named the name kwarg in your urls file.
It’s a keyword argument, not an argument. So you should type the keyword and the value.

For example your current urlpatterns list in one of your installed apps urls.py file looks like this:

    urlpatterns = [
       path('', views.index, 'index'),
       path('like/', views.like, 'like')
    ]

You should check if you have missed the name kwarg. The above code should be changed to:

    urlpatterns = [
        path('', views.index, name='index'),
        path('like/', views.like, name='like')
    ]

If you want to find it faster, you can comment each app’s url inclusion, in the your_project/urls.py file. When the error is gone, it means that you should check the commented app urls.py file.

24👍

Check if you have the name argument in all of your urls.py files, for each Django app you have installed.

If you have specified a name argument for any url in the path function, it should be declared like path('', views.a, name='view.a'), not like path('', views.a, 'view.a').

Notice the absence of the name argument in the latter code.
If you miss the name argument, you will get the 'TypeError at / 'str' object is not a mapping' error.

20👍

Please, check for errors in admin_llda.urls.
You might have missed adding name='' in one of the path() calls.

E.g.:

You might have written

path('',views.some_method, 'somename')

instead of path

path('',views.some_method, name= 'somename')
👤Vinay

2👍

I just had the same issue and I found the solution! Check your urls.py and whether you failed to name any url appropriately- not necessarily

0👍

I had the same issue , check the name argument in the path('',,name=" ")

👤Alami

0👍

Try adding namespace to your url
e.g add the following to your ‘my_app/urls.py’
app_name='my_app'

then your template should look something like: <a class="item" href="{% url 'my_app:home' %}">

finally be sure to register your app in the ‘my_project/settings.py’

https://docs.djangoproject.com/en/3.0/topics/http/urls/#naming-url-patterns

Leave a comment