繁体   English   中英

Django错误与参数'('',)'没有反向匹配

[英]Django error no reverse match with arguments '('',)'

用我的第一篇文章作为求助的呼声我感觉有点不好,但是嘿,我当然不是第一个 lulz,无论如何,我自学 python/django,我真的卡在 atm 并且我最近自己一直在努力解决问题,浪费了很多时间这样做,这个让我卡住了。

我收到错误消息:NoReverseMatch at /messages/newreply/1/ Reverse for 'newreply' with arguments '('',)' not found。 尝试了 1 种模式:['messages/newreply/(?P<post_id>[0-9]+)/\Z']

这是我的网址文件;

app_name = 'board'
urlpatterns = [
    path('', views.index, name='index'),
    path('<int:post_id>/', views.postdetail, name='detail'),
    path('newmsg/', views.newmsg, name='newmsg'),
    path('newreply/<int:post_id>/', views.newreply, name='newreply')

看法

def newreply(request, post_id):
    post = Post.objects.get(id=post_id)
    if request.method != "POST":
        form = ReplyForm()
    else:
        form = ReplyForm(data=request.POST)
        if form.is_valid():
            newreply= form.save(commit=False)
            newreply.post = post
            newreply.save()
            return redirect('board:index')
    context = {'form':form}
    return render(request, 'board/newreply.html', context)

模板;

{% extends 'pages/base.html' %}
{% block content %}

<p>Add your reply here</p>
<form action="{% url 'board:newreply' post.id %}"method ='post'>
    {% csrf_token %}
    {{ form.as_p }}
    <button name = "submit">Add post</button>
</form>
{% endblock content %}

我已经尝试了很多事情,现在我什至不知道我从哪里开始了,所以我真的很感激任何帮助,特别是知道为什么它实际上会发生,因为我阅读了一些具有相同类型错误的帖子,但尝试发布的解决方案却产生了其他错误我。

谢谢!

这确实是一个无用且烦人的错误。 通常,这意味着您没有在请求的上下文中传递某些内容。 在这种情况下, post对象。

context = {'form':form, 'post': post}
return render(request, 'board/newreply.html', context)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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