-
Notifications
You must be signed in to change notification settings - Fork 210
/
views.py
118 lines (97 loc) · 3.17 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- coding: utf-8 -*-
# Create your views here.
import json
from django.http import JsonResponse
from django.shortcuts import render, get_object_or_404, reverse
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
from blog.models import Article, Category, Comment
def index(request):
"""
博客首页
:param request:
:return:
"""
article_list = Article.objects.all().order_by('-date_time')[0:5]
return render(request, 'blog/index.html', {"article_list": article_list,
"source_id": "index"})
def articles(request, pk):
"""
博客列表页面
:param request:
:param pk:
:return:
"""
pk = int(pk)
if pk:
category_object = get_object_or_404(Category, pk=pk)
category = category_object.name
article_list = Article.objects.filter(category_id=pk)
else:
# pk为0时表示全部
article_list = Article.objects.all() # 获取全部文章
category = u''
return render(request, 'blog/articles.html', {"article_list": article_list,
"category": category,
})
def about(request):
return render(request, 'blog/about.html')
def archive(request):
article_list = Article.objects.order_by('-date_time')
return render(request, 'blog/archive.html', {"article_list": article_list})
def link(request):
return render(request, 'blog/link.html')
def message(request):
return render(request, 'blog/message_board.html', {"source_id": "message"})
@csrf_exempt
def getComment(request):
"""
接收畅言的评论回推, post方式回推
:param request:
:return:
"""
arg = request.POST
data = arg.get('data')
data = json.loads(data)
title = data.get('title')
url = data.get('url')
source_id = data.get('sourceid')
if source_id not in ['message']:
article = Article.objects.get(pk=source_id)
article.commenced()
comments = data.get('comments')[0]
content = comments.get('content')
user = comments.get('user').get('nickname')
Comment(title=title, source_id=source_id, user_name=user, url=url, comment=content).save()
return JsonResponse({"status": "ok"})
def detail(request, pk):
"""
博文详情
:param request:
:param pk:
:return:
"""
article = get_object_or_404(Article, pk=pk)
article.viewed()
return render(request, 'blog/detail.html', {"article": article,
"source_id": article.id})
def search(request):
"""
搜索
:param request:
:return:
"""
key = request.GET['key']
article_list = Article.objects.filter(title__icontains=key)
return render(request, 'blog/search.html',
{"article_list": article_list, "key": key})
def tag(request, name):
"""
标签
:param request:
:param name
:return:
"""
article_list = Article.objects.filter(tag__tag_name=name)
return render(request, 'blog/tag.html', {"article_list": article_list,
"tag": name})