Ivan De leon
Ivan De leon

Reputation: 251

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

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

Upvotes: 24

Views: 22222

Answers (6)

Alami
Alami

Reputation: 45

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

Upvotes: 0

Miguel Carvalhais Matos
Miguel Carvalhais Matos

Reputation: 1143

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.

Upvotes: 24

purvi gupta
purvi gupta

Reputation: 641

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.

Upvotes: 54

321 Cba
321 Cba

Reputation: 1

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

Upvotes: 0

Pharis Mukui
Pharis Mukui

Reputation: 63

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

Upvotes: 2

Vinay
Vinay

Reputation: 657

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

Upvotes: 20

Related Questions