对 'editPost' 进行反转,但未找到参数 '('',)'。尝试了 1 种模式:['editPost/(?P<postslug>[^/]+)/$']

Reverse for 'editPost' with arguments '('',)' not found. 1 pattern(s) tried: ['editPost/(?P<postslug>[^/]+)/$']

我在模板下方收到此错误

{% 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}} 并注释了 link 行只是为了确保 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')

谁能帮我找出错误?

将您的 url 模式更改为:

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

同时将 link 更改为 :

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

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