path('administration/delete-post/', administration_views.delete_upload, name='delete_upload'),path('administration/delete-post/<slug:post_slug>', administration_views.delete_upload, name='delete_upload'),出于某種原因,當(dāng)我添加刪除類時(shí),我不得不添加兩個(gè)url模式。當(dāng)我注釋掉第一條路徑時(shí),它給了我一個(gè)錯(cuò)誤Reverse for 'NoReverseMatch at /administration/delete_upload' with no arguments not found. 1 pattern(s) tried: ['administration/delete\\-post/(?P<post_slug>[-a-zA-Z0-9_]+)$']我甚至在html上陳述了鼻涕蟲。{% for post in posts %} <a href="{% url 'delete_upload' %}{{ post.slug }}">Delete Upload</a>{% endif %}使用兩個(gè)相同的URL模式,它以某種方式工作正常,但現(xiàn)在我想添加一個(gè)確認(rèn)頁(yè)面,它再次導(dǎo)致錯(cuò)誤。views.pydef delete_upload(request, post_slug): post = get_object_or_404(VideoPost, slug=post_slug) if request.method == "POST": post.delete() context = { "post": post } return render(request, "administration/confirm_delete.html", context)confirm_delete.html{% block content %}<form action="." method="POST">{% csrf_token %} <h1>Are you sure you want to delete</h1> <p>{{post.title}}</p> <a href="../">Cancel</a><input type="submit" value="Confirm"></form>{% endblock %}錯(cuò)誤TypeError at /administration/delete-post/delete_upload() missing 1 required positional argument: 'post_slug'它引導(dǎo)我使用url上的slug正確confirm_delete頁(yè)面,但是當(dāng)我單擊確認(rèn)時(shí),slug在URL上消失了,并且它導(dǎo)致了似乎的錯(cuò)誤。我看到了問(wèn)題,但我無(wú)法解決它...請(qǐng)幫忙。感謝您的任何幫助
2 回答

慕桂英546537
TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊
在文件中移除path('administration/delete-post/',urls.py
在 HTML 中,將你與網(wǎng)址命名空間一起傳遞templatepost.slug
{% for post in posts %}
<a href="{% url 'delete_upload' post.slug %}">Delete Upload</a>
{% endif %}

胡說(shuō)叔叔
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
您不正確地使用了 url 模板標(biāo)記。在網(wǎng)頁(yè)中,你需要替換
<a href="{% url 'delete_upload' %}{{ post.slug }}">Delete Upload</a>
跟
<a href="{% url 'delete_upload' slug=post.slug %}">Delete Upload</a>
添加回答
舉報(bào)
0/150
提交
取消