第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

喜歡按鈕 Django3 - KeyError at /nutriscore/exemple-1/

喜歡按鈕 Django3 - KeyError at /nutriscore/exemple-1/

HUH函數(shù) 2023-03-22 16:54:50
我希望你很好。我是 Python 的初學(xué)者,我正在嘗試在這樣的博客文章中實(shí)現(xiàn)一個(gè)贊按鈕。在管理部分,我可以看到誰(shuí)在點(diǎn)擊類(lèi)似的內(nèi)容。但是我遇到兩個(gè)問(wèn)題:第一個(gè):當(dāng)我點(diǎn)擊“贊”按鈕時(shí),“返回”使用這種 url nutriscore/4/ 重定向我,或者我的文章使用了一個(gè) slug(比如 /nutriscore/exemple-1/。你有什么想法嗎?第二個(gè):當(dāng)我想顯示喜歡的數(shù)量時(shí) {{ total_likes }} 我有這個(gè)問(wèn)題:KeyError at /nutriscore/exemple-1/ 'pk'Models.py:class Post(models.Model):    ...    likes = models.ManyToManyField(User, related_name='nutriscore_posts')        def total_likes(self):        return self.likes.count()Views.py:class PostList(generic.ListView):    queryset = Post.objects.filter(status=1).order_by('-created_on')    template_name = 'index.html'def LikeView(request, pk):    post = get_object_or_404(Post, id=request.POST.get('post_id'))    post.likes.add(request.user)    return HttpResponseRedirect(reverse('post_detail', args=[str(pk)]))class PostDetail(generic.DetailView):    model = Post    context_object_name = 'post'    template_name = 'post_detail.html'        def get_context_data(self, **kwargs):        context = super(PostDetail, self).get_context_data(**kwargs)        stuff = get_object_or_404(Post, id=self.kwargs['pk'])        total_likes = stuff.total_likes        context['total_likes'] = total_likes        return contexturls.pypath('like/<int:pk>', LikeView, name="like_post"),post_detail.html    <form action="{% url 'like_post' post.pk %}" method="POST">{% csrf_token %}<button type="submit" name="post_id" value="{{ post.id }}" class="cherry-likes"><img src="static/img/like.png" width="30px" height="30px" class="" title=""></button></form>多謝 :)
查看完整描述

2 回答

?
慕蓋茨4494581

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊

您pk應(yīng)該是int。您還在您的網(wǎng)址中指定為 int,但在視圖中沒(méi)有指定。


post = get_object_or_404(Post, id=pk )

if request.method == "POST": 

     post.likes.add(request.user)

     return redirect('post_detail', post.pk)

要顯示帖子的總贊數(shù),您需要添加property這樣的裝飾器


@property

def total_likes(self):

    return self.likes.count()

現(xiàn)在在詳細(xì)信息模板{{post.total_likes}}中將顯示結(jié)果。


無(wú)需編寫(xiě)get_context_data方法來(lái)顯示 total_likes。


查看完整回答
反對(duì) 回復(fù) 2023-03-22
?
至尊寶的傳說(shuō)

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊

第一個(gè)問(wèn)題:你將 pk 傳遞給重定向,它是一個(gè)整數(shù)(根據(jù)你的 urls.py)到 url: args=[str(pk)]。如果你想重定向到一個(gè) url,/nutriscore/exemple-1你應(yīng)該傳遞 slug 而不是 pk(我猜這是 id)并相應(yīng)地調(diào)整你的 urls.py。

第二個(gè)問(wèn)題:你確定那個(gè)帖子有一個(gè)名為 pk 的屬性/鍵嗎?這不應(yīng)該是id嗎?喜歡:

<form action="{% url 'like_post' post.id %}" method="POST">{% csrf_token %}<button type="submit" name="post_id" value="{{ post.id }}" class="cherry-likes"><img src="static/img/like.png" width="30px" height="30px" class="" title=""></button></form>



查看完整回答
反對(duì) 回復(fù) 2023-03-22
  • 2 回答
  • 0 關(guān)注
  • 136 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)