from django.shortcuts import render, HttpResponse from home.models import * from comment.models import comment # Create your views here. def index(request): artchle = Articles.objects.all() artchles = {"artchles": []} for i in artchle: a = { "id":i.id, "title": i.title, "abstract": i.abstract, "created": i.created, "stat": i.stat, "read": i.read } artchles["artchles"].append(a) return render(request, 'index.html', context=artchles) def archives(request, id): i = Articles.objects.get(id=id) i.read += 1 i.save() previous_article = Articles.objects.filter(id__lt=id).order_by('-id').first() previous_id = previous_article.id if previous_article else id previous_id_title = previous_article.title if previous_article else "暂无上一篇" next_article = Articles.objects.filter(id__gt=id).order_by('id').first() next_id = next_article.id if next_article else id next_id_title = next_article.title if next_article else "暂无下一篇" comments = comment.objects.filter(archives_Id=id).order_by("-comment_Time").all() a = { "id": i.id, "title": i.title, "abstract": i.abstract, "created": i.created, "stat": i.stat, "read": i.read, "content": i.content, "author": i.author, "previous":{ "id": previous_id, "title": previous_id_title, },"next":{ "id": next_id, "title": next_id_title, },"comments": [] } for c in comments: com = { "id": c.id, "comment_Content": c.comment_Content, "comment_User": c.comment_User, "comment_Time": c.comment_Time, "qq": c.qq, } a["comments"].append(com) return render(request, 'archives.html', a)