简体   繁体   中英

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

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.

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.

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

Eg:

You might have written

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

instead of path

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

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

我有同样的问题,检查路径中的“名称”参数('',,名称 =“”)

Try adding namespace to your url eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM