我目前有一个模型叫做Comments.
在表单中输入 youtube 频道后,用户将被带到索引模板,该模板显示该 youtube 频道视频的所有评论,其中包括三个关键字之一(关键字 A、关键字 B、关键字 C)。
我想添加一项功能,因此页面顶部有三个链接/按钮(每个按钮对应一个关键字)。
用户可以按下该链接而无需重新加载页面(这是否意味着我将需要 AJAX?)查看带有该关键字的评论,而不是使用三个关键字中的任何一个的所有评论。
我目前正在将四个内容变量对象从视图发送到模板(一个包含所有注释,另外三个对象每个只包含该关键字的注释对象)。
所以模板可以访问我需要的数据,我只需要制作它,以便在单击链接/按钮之一时,它只显示该内容。
意见
def addTodo(request):
new_item =Channel(channel=request.POST['channel'])
#if channel exists render page with comments
if Channel.objects.filter(channel=new_item.channel).exists():
channel_obj=Channel.objects.get(channel=request.POST['channel'])
comments_object=Comments.objects.filter(channel=channel_obj)
comments_objectA=Comments.objects.filter(channel=channel_obj, key="keywordA")
comments_objectB=Comments.objects.filter(channel=channel_obj, key="keywordB")
comments_objectC=Comments.objects.filter(channel=channel_obj, key="keywordC")
return render(request, 'todo/index.html', {'comments_all': comments_object, 'commentsa': comments_objectA,'commentsb': comments_objectB,'commentsc': comments_objectC})
索引模板
#three buttons/links on top to allow user to sort..the part Im not sure how to do:
<button type="button"onclick="justshowrelatedcomment>KeywordA!</button>
<button type="button"onclick="justshowrelatedcomment>KeywordB</button>
<button type="button" onclick="justshowrelatedcomment>KeywordC</button>
#the comment structure, would want to replace comments_all with whatever button is clicked on.
<div class="new_comment">
<!-- build comment -->
{%for a in comments_all%}
<ul class="user_comment">
<!-- current #{user} avatar -->
<!-- the comment body --><div class="comment_body">
<p>{{ a.question }}</p>
</div>
</ul>
{% endfor %}
</div>
</div>
我很困惑..没有Ajax这可能吗?
如果 Ajax 是我唯一/最好的选择,我应该怎么做?
我使用这个解决方案来避免 ajax,因为我不知道如何使用 ajax。
谢谢和欢呼。