为了账号安全,请及时绑定邮箱和手机立即绑定

django 无反向匹配在 / 类型错误 - 缺少所需的位置参数

django 无反向匹配在 / 类型错误 - 缺少所需的位置参数

蝴蝶刀刀 2022-09-27 16:16:17
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'),出于某种原因,当我添加删除类时,我不得不添加两个url模式。当我注释掉第一条路径时,它给了我一个错误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 %}使用两个相同的URL模式,它以某种方式工作正常,但现在我想添加一个确认页面,它再次导致错误。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 %}错误TypeError at /administration/delete-post/delete_upload() missing 1 required positional argument: 'post_slug'它引导我使用url上的slug正确confirm_delete页面,但是当我单击确认时,slug在URL上消失了,并且它导致了似乎的错误。我看到了问题,但我无法解决它...请帮忙。感谢您的任何帮助
查看完整描述

2 回答

?
慕桂英546537

TA贡献1848条经验 获得超10个赞

在文件中移除path('administration/delete-post/',urls.py


在 HTML 中,将你与网址命名空间一起传递templatepost.slug


{% for post in posts %}

    <a href="{% url 'delete_upload' post.slug %}">Delete Upload</a>

{% endif %}


查看完整回答
反对 回复 2022-09-27
?
胡说叔叔

TA贡献1804条经验 获得超8个赞

您不正确地使用了 url 模板标记。在网页中,你需要替换

<a href="{% url 'delete_upload' %}{{ post.slug }}">Delete Upload</a>

<a href="{% url 'delete_upload' slug=post.slug %}">Delete Upload</a>



查看完整回答
反对 回复 2022-09-27
  • 2 回答
  • 0 关注
  • 109 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号