简体   繁体   中英

django NoReverseMatch at / TypeError - missing required positional arguments

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'),

For some reason, when I added delete class, I had to add two url patterns. When I comment out the first path, It gives me an error of

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_]+)$']

I even stated the slugs on html.

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

With two same url patterns, it worked fine somehow, but now I want to add a confirmation page, and it's causing an error again.

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 %}

error

TypeError at /administration/delete-post/
delete_upload() missing 1 required positional argument: 'post_slug'

It directs me to confirm_delete page correctly with the slugs on url, but when I click confirm, the slug is gone on the url and it's causing the error it seems.

I see the problem, but I can't fix it... please help. Thank you for any helps

Remove path('administration/delete-post/', in your urls.py file

In your HTML template pass your post.slug along with the url namespace

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

You are using url template tag incorrectly. In html you need replace

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

with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM