本文介绍了未找到参数为 '('',)' 的 'update_comment' 反转.尝试了 1 个模式:['comment\/(?P<news_pk>[0-9]+)$']的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个新闻网站.现在我正在详细说明评论发布功能.遇到问题说:

I'm coding a news site.Now I'm detailing with the comment post function.And meet the issue says:

Reverse for 'update_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['comment\/(?P<news_pk>[0-9]+)$']

我已经尝试了很多方法和时间,但仍然无法解决它,我找不到我的代码有任何问题.真的需要你的帮助.

I have tried many ways and times but still can't solve it and I can't find any wrong with my code.And really need your help.

评论发布功能在 news_detail.html 中.我的项目中有两个重要的应用新闻操作评论模型在操作

The comment post function is in the news_detail.html. There are two important apps in my project news and operation comment models is under operation

这是我的root urls.py:

path('news', include(('news.urls', 'news'), namespace="news")),
path('', include(('operation.urls', 'operation'), namespace="operation")),

这是news/urls.py

path('-<int:news_pk>', newsDetailView, name="news_detail")

这里是操作/urls.py:

Here is the operation/urls.py:

path('comment/<int:news_pk>', views.update_comment, name="update_comment"),

这是news/view.py

def newsDetailView(request, news_pk):
    news = News.objects.get(id=news_pk)
    title = news.title
    author = news.author_name
    add_time = news.add_time
    content = news.content
    category = news.category
    tags = news.tag.annotate(news_count=Count('news'))

    all_comments = NewsComments.objects.filter(news=news)
    return render(request, "news_detail.html", {
        'title': title,
        'author': author,
        'add_time': add_time,
        'content': content,
        'tags': tags,
        'category': category,
        'all_comments': all_comments,
    })

这里是operation/views.py

def update_comment(request, news_pk):
    news = News.objects.get(id=news_pk)
    comment_form = CommentForm(request.POST or None)
    if request.method == 'POST' and comment_form.is_valid():
        if not request.user.is_authenticated:
            return render(request, 'login.html', {})
        comments = comment_form.cleaned_data.get("comment")
        news_comment = NewsComments(user=request.user, comments=comments, news=news)
        news_comment.save()

        return render(request, "news_detail.html", {
            'news_comment': news_comment,
            'news':news
        })

这里是 news_detail.html:

And here is the news_detail.html:

{% if user.is_authenticated %}

<form method="POST" action="{% url 'operation:update_comment' news.pk %}">{% csrf_token %}              

<textarea id="js-pl-textarea" name="comment"></textarea>      

<input type="submit" id="js-pl-submit" value="发表评论"></input></form>

推荐答案

您没有将 news 对象传递到 news_detail.html 的上下文中.您可以通过传递 news 并在模板中执行诸如 {{ news.title }} 之类的操作(而不是 {{ title }}):

You're not passing the news object into the context for news_detail.html. You can simplify the view a lot by just passing news and doing things like {{ news.title }} in the template (instead of {{ title }}):

def newsDetailView(request, news_pk):
    news = get_object_or_404(News, id=news_pk)
    tags = news.tag.annotate(news_count=Count('news'))
    all_comments = NewsComments.objects.filter(news=news)

    return render(request, "news_detail.html", {
        'news': news,
        'tags': tags,
        'all_comments': all_comments,
    })

现在 news.pk 将作为 {% url ... %} 标签的参数.如果找不到 news 对象(在您的代码中,它会崩溃),我还确保生成 404 错误.

Now news.pk will work as argument to your {% url ... %} tag. I've also ensured a 404 error is generated if the news object cannot be found (in your code, it would crash).

这篇关于未找到参数为 '('',)' 的 'update_comment' 反转.尝试了 1 个模式:['comment\/(?P&lt;news_pk&gt;[0-9]+)$']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 20:39