TypeError at / 'str' object is not a mapping in django template

17,009

Solution 1

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.

Solution 2

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.

Solution 3

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')

Solution 4

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

Share:
17,009

Related videos on Youtube

Ivan De leon
Author by

Ivan De leon

Updated on September 14, 2022

Comments

  • Ivan De leon
    Ivan De leon over 1 year

    I am trying to set up links inside a tags, and when I do this procedure as seen in the code, it gives me the error:

    TypeError at / 'str' object is not a mapping

    It use to work fine but then decided not to

    template code:

    <a class="item" href="{% url 'home' %}">
    

    urls code:

    urlpatterns = [
      path('admin/', include('admin_llda.urls') ),
      path('about/', views.about, name = 'about'),
      path('dashboard/',views.dashboard, name = 'dashboard'),
      path('',views.homepage, name = 'home')   
    ]
    
    • Willem Van Onsem
      Willem Van Onsem over 5 years
      It looks like there is an error in your homepage view.
    • Evhz
      Evhz over 5 years
      As you say it worked before, maybe is a problem with the url you try to visit. Anyway try to update your template with: <a class="item" href="{{ url }}">Home</a>
  • Aleksei Khatkevich
    Aleksei Khatkevich almost 5 years
    path("", ApiRoot.as_view(), name=ApiRoot.name) .Doesntowork
  • Aleksei Khatkevich
    Aleksei Khatkevich almost 5 years
    url(r'^$', ApiRoot.as_view(), name=ApiRoot.name), surprisingly does work....
  • Vega
    Vega almost 5 years
    This answer has been given stackoverflow.com/a/55865069/5468463