简体   繁体   English

TypeError at /'str' 对象不是 django 模板中的映射

[英]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 /'str' 对象处的 TypeError 不是映射

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.检查您是否在urls文件中正确命名了name kwarg。 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:例如,您已安装的应用程序urls.py文件之一中的当前urlpatterns列表如下所示:

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

You should check if you have missed the name kwarg.你应该检查你是否错过了 kwarg 这个name 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.如果你想更快地找到它,你可以在your_project/urls.py文件中评论每个应用程序的 url 包含。 When the error is gone, it means that you should check the commented app urls.py file.当错误消失时,这意味着您应该检查注释的应用程序urls.py文件。

Check if you have the name argument in all of your urls.py files, for each Django app you have installed.检查您的所有urls.py文件中是否有name参数,适用于您安装的每个 Django 应用程序。

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') .如果您在path函数中为任何 url 指定了名称参数,则应将其声明为path('', views.a, name='view.a') ,而不是path('', views.a, 'view.a')

Notice the absence of the name argument in the latter code.注意后面的代码中没有name参数 If you miss the name argument, you will get the 'TypeError at / 'str' object is not a mapping' error.如果您错过了 name 参数,您将收到'TypeError at / 'str' object is not a mapping'错误。

Please, check for errors in admin_llda.urls .请检查admin_llda.urls错误。 You might have missed adding name='' in one of the path() calls.您可能没有在path()调用之一中添加name=''

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检查您的 urls.py 以及您是否未能正确命名任何网址 - 不一定

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

Try adding namespace to your url eg add the following to your 'my_app/urls.py' app_name='my_app'尝试将命名空间添加到您的 url,例如将以下内容添加到您的 'my_app/urls.py' app_name='my_app'

then your template should look something like: <a class="item" href="{% url 'my_app:home' %}">那么你的模板应该看起来像: <a class="item" href="{% url 'my_app:home' %}">

finally be sure to register your app in the 'my_project/settings.py'最后一定要在“my_project/settings.py”中注册你的应用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:“str”对象不可调用 - Django - TypeError: 'str' object is not callable - Django TypeError:&#39;str&#39;对象不可调用(Django / Python) - TypeError: 'str' object is not callable (Django/Python) Django 错误:/forums/'str' object 处的 TypeError 不可调用 - Django error: TypeError at /forums/ 'str' object is not callable Django 获取 TypeError:JSON 对象必须是 str、bytes 或 bytearray,而不是 dict - Django get TypeError: the JSON object must be str, bytes or bytearray, not dict Django TypeError:预期的 str、bytes 或 os.PathLike object,不是 NoneType - Django TypeError: Expected str, bytes or os.PathLike object, not NoneType Django表单验证错误-TypeError&#39;str&#39;对象不可调用 - Django Forms validation error - TypeError 'str' object is not callable Django Rest Framework TypeError需要一个类似字节的对象,而不是&#39;str&#39; - Django Rest Framework TypeError a bytes-like object is required, not 'str' TypeError:预期的 str、bytes 或 os.PathLike 对象,而不是 Django 中的元组 - TypeError: expected str, bytes or os.PathLike object, not tuple in Django &#39;str&#39;对象不可调用TypeError - 'str' object is not callable TypeError TypeError:&#39;str&#39;对象不可调用 - TypeError: 'str' object is not callable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM