Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

python - Reverse for 'update_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['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.

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

Here is my root urls.py:

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

Here is the news/urls.py

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

Here is the operation/urls.py:

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

Here is the 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,
    })

Here is 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
        })

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...