社区部分功能的开发
包括发帖,回帖,以及点赞功能的实现
以下代码是django view.py 中的相关视图的定义
# 题目列表界面类
# 模板文件是‘discussions.html’
# 采用django自带的列表分页类
# 分页个数是 5
# 展示用户发的帖子
# 可以发布新的帖子
class DiscussionView(generic.ListView):
model = Discussion
template_name = 'judgeOL/discussions.html'
context_object_name = 'discussion_list'
paginate_by = 5
def get_queryset(self):
problem_id = self.kwargs['problem_id']
return Discussion.objects.filter(problem_id=problem_id)
# 往context中写入额外信息
def get_context_data(self, **kwargs):
context = super(DiscussionView, self).get_context_data(**kwargs)
# 往context中写入题目信息
context['problem_id'] = self.kwargs['problem_id']
context['problem_name'] = Problem.objects.get(pk=self.kwargs['problem_id']).name
return context
# 题目列表界面类
# 模板文件是‘responses.html’
# 采用django自带的列表分页类
# 分页个数是 5
# 展示用户的回复
# 可以编辑新的回复
class ResponseView(generic.ListView):
model = Response
template_name = 'judgeOL/responses.html'
context_object_name = 'response_list'
paginate_by = 5
def get_queryset(self):
return Response.objects.all().filter(discussion_id=self.kwargs['discussion_id'])
# 往context中写入额外信息
def get_context_data(self, **kwargs):
context = super(ResponseView, self).get_context_data(**kwargs)
discussion = Discussion.objects.get(pk=self.kwargs['discussion_id'])
# 往context中写入帖子信息
context['discussion'] = discussion
# 往context中写入题目信息
context['problem_id'] = self.kwargs['problem_id']
# 题目的访问量增加1
discussion.view_count += 1
discussion.save()
return context
# 新帖子编辑界面
# 模板文件是‘edit.html’
class EditView(generic.TemplateView):
template_name = 'judgeOL/edit.html'
# 往context中写入额外信息
def get_context_data(self, **kwargs):
context = super(EditView, self).get_context_data(**kwargs)
# 往context中写入题目信息
context['problem_id'] = self.kwargs['problem_id']
return context
再者是django url.py 中的相关路径的定义
# ex: /judgeOL/discussions
url(r'^(?P<problem_id>[0-9]+)/discussions/$',
views.DiscussionView.as_view(), name='discussions'),
# ex: /judgeOL/new_discussion
url(r'^(?P<problem_id>[0-9]+)/new_discussion/$',
views.new_discussion, name='new_discussion'),
# ex: /judgeOL/edit
url(r'^(?P<problem_id>[0-9]+)/edit/$',
views.EditView.as_view(), name='edit'),
# ex: /judgeOL/responses
url(r'^(?P<problem_id>[0-9]+)/(?P<discussion_id>[0-9]+)/responses/$',
views.ResponseView.as_view(), name='responses'),
# ex: /judgeOL/new_response
url(r'^(?P<problem_id>[0-9]+)/(?P<discussion_id>[0-9]+)/new_response/$',
views.new_response, name='new_response'),
# ex: /judgeOL/vote
url(r'^vote/$', views.vote, name='vote'),
然后是 Html 页面。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<title>Signin Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link href="../../../static/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">judgeOL</span>
<span class="icon-bar