def detail(request, head_line, error_form=None): # print('get head_line:', head_line) context = {} form = CommentForm article = Article.objects.get(headline_2=head_line) print('article is:', article) voter_id = request.user.profile.id user_ticket_for_this_article = [] try: user_ticket_for_this_article = Ticket.objects.get(voter_id=voter_id, article=article) best_comment = Comment.objects.filter(is_best='True', belong_to=article) user_ticket_for_this_article.save() except: new_ticket = Ticket(voter_id=voter_id, article_id=id, choice=request.POST['vote']) new_ticket.save() # print ('user_ticket is:', user_ticket_for_this_article) context['article'] = article context['user_ticket'] = user_ticket_for_this_article if best_comment: context['best_comment'] = best_comment[0] if error_form is not None: context['form'] = error_form else: context['form'] = form return render(request, 'huoyan_article.html', context)
def vote(request, id): # 未登录用户不允许投票,自动跳回详情页 if not isinstance(request.user, User): return redirect(to="detail", id=id) voter_id = request.user.id try: # 如果找不到登陆用户对此篇文章的操作,就报错 user_ticket_for_this_article = Ticket.objects.get(voter_id=voter_id, article_id=id) user_ticket_for_this_article.choice = request.POST["vote"] user_ticket_for_this_article.save() except ObjectDoesNotExist: new_ticket = Ticket(voter_id=voter_id, article_id=id, choice=request.POST["vote"]) new_ticket.save() # 如果是点赞,更新点赞总数 if request.POST["vote"] == "like": article = Article.objects.get(id=id) article.likes += 1 article.save() return redirect(to="detail", id=id)
def vote(request, id): # ---------------------------------作业代码-开始--------------------------------- # 因为vote后必然重定向至详情页,未避免浏览量无端+1,在此先-1,且不必判断下限 article = Article.objects.get(id=id) article.views -= 1 article.save() # ---------------------------------作业代码-结束--------------------------------- # 未登录用户不允许投票,自动跳回详情页 if not isinstance(request.user, User): return redirect(to="detail", id=id) voter_id = request.user.id try: # 如果找不到登陆用户对此篇文章的操作,就报错 user_ticket_for_this_article = Ticket.objects.get(voter_id=voter_id, article_id=id) user_ticket_for_this_article.choice = request.POST["vote"] user_ticket_for_this_article.save() except ObjectDoesNotExist: new_ticket = Ticket(voter_id=voter_id, article_id=id, choice=request.POST["vote"]) new_ticket.save() # ---------------------------------作业代码-开始--------------------------------- # 如果是点赞,且该篇文章 未被 请求点赞用户点赞过,则更新点赞总数 # 并将点赞用户request.user.userprofile(即UserProfile)添加到Artical.collected_by字段中 # 如果是点赞,且该篇文章 已被 请求点赞用户点赞过,则无所作为 if request.POST["vote"] == "like": try: # 若已点过赞 article = Article.objects.get(id=id, collected_by=request.user.profile) # 则无所作为 except ObjectDoesNotExist: # 若未点过赞 # 则做点什么 article = Article.objects.get(id=id) article.likes += 1 article.collected_by.add(request.user.profile) article.save() # 如果是点炸,且该篇文章 已被 请求点赞用户点赞过,则更新点赞总数 # 并将点炸用户request.user.userprofile(即UserProfile)排除在Artical.collected_by字段外 if request.POST["vote"] == "dislike": try: # 若已点过赞 article = Article.objects.get(id=id, collected_by=request.user.profile) # 则做点什么 if article.likes > 0: article.likes -= 1 article.collected_by.remove(request.user.profile) article.save() except ObjectDoesNotExist: # 若已点过赞 # 则无所作为 pass # ---------------------------------作业代码-结束--------------------------------- return redirect(to="detail", id=id)
def detail_vote(request,id): voter_id = request.user.profile.id try: user_ticker_for_this_article = Ticket.objects.get(voter_id = voter_id,article_id = id) user_ticker_for_this_article.choice = request.POST['vote'] user_ticker_for_this_article.save() except ObjectDoesNotExist: new_ticket = Ticket(voter_id=voter_id,article_id=id,choice=request.POST['vote']) new_ticket.save() return redirect(to='detail',id=id)
def detail_vote(request, id): voter_id = request.user.profile.id vote = request.POST['vote'] try: user_ticket_for_this_video = Ticket.objects.get(voter_id=voter_id, video_id=id) user_ticket_for_this_video.choice = vote user_ticket_for_this_video.save() except ObjectDoesNotExist: new_ticket = Ticket(voter_id=voter_id, video_id=id, choice=vote) new_ticket.save() # return redirect(to='detail', id) return redirect(to='detail', id=id)
def detail_vote(request, page_num): voter_id = request.user.profile.id try: user_ticket_for_this_video = Ticket.objects.get(voter_id=voter_id, article_id=page_num) user_ticket_for_this_video.choice = request.POST['vote'] user_ticket_for_this_video.save() except ObjectDoesNotExist: new_ticket = Ticket(voter_id=voter_id, article_id=page_num, choice=request.POST['vote']) new_ticket.save() return redirect(to='detail', page_num=page_num)
def detail_vote(request, head_line): voter_id = request.user.profile.id article = Article.objects.get(headline_2=head_line) try: user_ticket_for_this_article = Ticket.objects.get(voter_id=voter_id, article=article) user_ticket_for_this_article.choice = request.POST['vote'] user_ticket_for_this_article.save() except ObjectDoesNotExist: new_ticket = Ticket(voter_id=voter_id, article=article, choice=request.POST['vote']) new_ticket.save() return redirect(to='detail', head_line=head_line)
def detail_vote(request, id): voter_id = request.user.profile.id try: user_ticket_for_video = Ticket.objects.get(voter_id=voter_id, article_id=id) user_ticket_for_video.choice = request.POST['vote'] user_ticket_for_video.save() except ObjectDoesNotExist: new_ticket = Ticket(voter_id=voter_id, video_id=id, choice=request.POST('vote')) new_ticket.save(force_insert=False, force_update=False, using=None, update_fields=None) return redirect(to='detail', id=id)
def vote(request, id): voter_id = request.user.id try: # 如果找不到登陆用户对此篇文章的操作,就报错 user_ticket_for_this_article = Ticket.objects.get(voter_id=voter_id, article_id=id) user_ticket_for_this_article.choice = request.POST["vote"] user_ticket_for_this_article.save() except ObjectDoesNotExist: new_ticket = Ticket(voter_id=voter_id, article_id=id, choice=request.POST["vote"]) new_ticket.save() article = Article.objects.get(id=id) article.likes = Ticket.objects.filter(article=id, choice='like').count() article.save() return redirect(to="detail", id=id)
def vote(request, id): if not isinstance(request.user, User): return redirect(to='detail', id=id) voter_id = request.user.profile.id # print(request.POST) try: user_ticket_for_article = Ticket.objects.get(voter_id=voter_id, article_id=id) user_ticket_for_article.choice = request.POST['vote'] user_ticket_for_article.save() except ObjectDoesNotExist: new_ticket = Ticket(voter_id=voter_id, article_id=id, choice=request.POST['vote']) new_ticket.save() if request.POST['vote'] == 'like': article = Article.objects.get(id=id) article.likes += 1 article.save() return redirect(to='detail', id=id)