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

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.py


def 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上消失了,并且它导致了似乎的错误。


我看到了问题,但我无法解决它...请帮忙。感谢您的任何帮助


蝴蝶刀刀
浏览 110回答 2
2回答

慕桂英546537

在文件中移除path('administration/delete-post/',urls.py在 HTML 中,将你与网址命名空间一起传递templatepost.slug{% for post in posts %}&nbsp; &nbsp; <a href="{% url 'delete_upload' post.slug %}">Delete Upload</a>{% endif %}

胡说叔叔

您不正确地使用了&nbsp;url&nbsp;模板标记。在网页中,你需要替换<a&nbsp;href="{%&nbsp;url&nbsp;'delete_upload'&nbsp;%}{{&nbsp;post.slug&nbsp;}}">Delete&nbsp;Upload</a>跟<a&nbsp;href="{%&nbsp;url&nbsp;'delete_upload'&nbsp;slug=post.slug&nbsp;%}">Delete&nbsp;Upload</a>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python