【问题标题】:Reverse for 'editPost' with arguments '('',)' not found. 1 pattern(s) tried: ['editPost/(?P<postslug>[^/]+)/$']未找到带有参数 '('',)' 的 'editPost' 的反向操作。尝试了 1 种模式:['editPost/(?P<postslug>[^/]+)/$']
【发布时间】:2019-12-07 05:36:24
【问题描述】:

我在下面的模板中收到此错误

{% for post in post_queryset %}
                <div class="card-body">
                  <div class="date">{{ post.created_date }}</div>
                  <div class="title">
                    {{ post.text }}
                    {{ post.slug }}
                    <a href="{% url 'editPost' post.slug %}" ><i class="fa fa-edit"></i></a>
                    <a onClick="delete_post('{{post.slug}}','{{post_id}}')"><i class="fa fa-trash-o"></i></a>
                  </div>
                </div>
{% endfor %}

我在这一行得到错误

<a href="{% url 'editPost' post.slug %}" ><i class="fa fa-edit"></i></a>

我在此行之前显示 {{post.slug}} 并评论链接行只是为了确保 post.slug 有一些内容。看起来 post.slug 有有效的 slug 信息。

我也尝试只传递一些字符串而不是像下面那样的 post.slug 然后它正在工作

<a href="{% url 'editPost' 'some_string' %}" ><i class="fa fa-edit"></i></a>

我的 urls.py 如下所示

path('editPost/<postslug>/',views.editPost, name='editPost')

谁能帮我找出错误?

【问题讨论】:

    标签: django django-templates django-urls


    【解决方案1】:

    将您的网址格式更改为:

    path('editPost/<slug:postslug>/',views.editPost, name='editPost')
    

    同时将链接更改为:

    <a href="{% url 'editPost' some_string %}" >
    

    【讨论】:

    【解决方案2】:

    我收到错误是因为在所有帖子中,有一篇帖子的 slug 字段为空,这导致了错误。

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 2021-03-26
      • 1970-01-01
      • 2021-12-20
      • 2020-11-05
      • 2018-11-21
      • 1970-01-01
      • 2023-03-28
      • 2021-04-24
      相关资源
      最近更新 更多