python小打小闹之简陋版BBS
闲的蛋疼,索性写个东西玩,于是,写个类似于BBS的念头就开始了。
我们考虑到需要实现的功能:
1 只有登陆的用户才可以点赞,如果没有登陆,那么不可以点赞,点赞次数只可以为1。
2 只有登陆的用户可以评论文章,其他用户也可通过回复其他人的评论。
3 评论之间有多级嵌套关系,不是单独存在的,比我评论文章A,别人在我的评论基础上再评论我的评论,那么我的评论和别人评论我的评论之间存在一层关系。
4 用户可以增删改查自己的文章,可以搜索自己的文章。文章包含三种状态,发布,草稿,隐藏。
5 每篇文章需要显示发布日期,发布人,点赞次数,评论次数,在首页展示的时候能够图文并茂展示,通过文章优先级来让部分文章置顶显示。
6 文章要有归属模块,页面展示出板块
7 and so on ...
我个人觉得只要实现上述功能,BBS的雏形就出来了。那么开工吧。
所需要的知识:
1 django+python
2 js
3 jinja
=================================================================================================
首先这个毫无疑问使用的Django 作为我的web frame。
第一要解决的是数据库关系,此时我拿Django默认的sqilite作为我的数据库,数据库关系我的想法是:
文章表:用户名(外键关联用户表), 发布时间,修改时间,状态,文章内容,文章归属模块,内容简介,优先级,文章头像
评论表:评论时间,是点赞还是评论,评论内容,归属哪篇文章(关键文章表),父籍评论ID
版块表:名字,简介,置顶文章,版块位置。
用户表: 关联Django系统自带的用户,用户名。
数据库建表关系解决 后,那就初始化,数据库。。。。。。。。
代码我在此我就简单说说几点:
1 点赞次数限制:
说到点赞,我们采用三个值来确定这个用户是否点赞过一次了,文章ID+用户ID+当前点赞次数
我们在评论表存储这个用户点赞次数的时候,连同用户ID一块存储进去,所以我们单单过滤这个表就可以了。
前端代码:
<script src="/static/bootstrap/js/jquery-2.1.4.js"></script>
<script>
//获取点赞数的函数
function Get_Praise(article_id,ele){
$.get("{% url "post_comment" %}",
{'id':article_id,'choice':2}, //紧跟的是参数
function (data) {
$(ele).html(' '+data)
}
)
}
//提交点赞请求
function click_praise(ele){
var article_id = $(ele).siblings().first().attr('article_id');
var GetCsrf = $("input[name='csrfmiddlewaretoken']").val();
// begin post
$.post("{% url 'post_comment' %}",
{
'comment_type':2,
'article':article_id,
'user':'{{ request.user.userprofile.id }}',
'csrfmiddlewaretoken': GetCsrf,
}, //end post
function(callback){
// 如果返回来的参数是limit说明这个用户被限制点赞了,因为点赞次数超过1次了
if(callback=='error'){
alert('点赞失败,可能是网络问题导致的')
}else if(callback=='limit'){
alert('您已经点赞过一次了,请勿重复点赞!!')
} else{
Get_Praise(article_id,ele)
}
}
)
}
后端代码,
praise_obj = models.Article.objects.filter(id=article_id)
for pb in praise_obj:
# 通过反向查询,来获取comment表里面的内容,表后面必须紧接着 _set !
pb_obj = pb.comment_set.select_related()
# get the praise's number,and count that!
praise = pb_obj.filter(comment_type=comment_type).count()
return HttpResponse(praise)
2 只有可以登陆才可以评论,并且在评论框中点击登陆,登陆成功后自动跳转到刚才的页面,跳转的关键点在于?next,通过这个我们后端可以让用户重定向到next指定的页面:
前端代码展示:
<div class="comment-box">
{% if request.user.is_authenticated %}
<textarea class="form-control" rows="3"></textarea>
{% csrf_token %}
<button type="button" style="margin-top: 10px" class="btn btn-success pull-right">评论</button>
{% else %}
<div class="jumbotron">
<h4 class="text-center"><a class="btn-link" href="{% url 'login' %}?next={{ request.path }}">登录</a>后评论</h4>
</div>
{% endif %} </div>
后端代码展示:
if request.method == 'POST':
user = authenticate(username=request.POST.get('username'),
password=request.POST.get('password'))
if user is not None:
#pass authentication
login(request,user)
return HttpResponseRedirect(request.GET.get('next') or '/bbs')
3 评论多级嵌套关系:
公司事今天有点多,今晚再弄,
4 用户新建一篇自己的文章,通过form来创建表单
前端代码如下:
<form class="form-horizontal" method="post" enctype="multipart/form-data">{% csrf_token %}
<div class="form-group">
// 遍历form的值。
{% for info in article %}
{% if info.field.required %}
<label style="font-size: larger">{{ info.label }}*</label></br>
{% else %}
{{ info.label }}</br>
{% endif %}
{{info}}</br>
{% endfor %}
<input name="author" style="display: none;" value="{{ request.user.userprofile.id }}">
</div>
<div class="col-sm-11">
<input type="submit" class="btn btn-primary " value="Save">
<a href="/person_zone/{{ request.user.userprofile.id }}" class="btn btn-danger">Cancel</a>
</div>
</form>
{% block bottom-js %}
<script src="/static/bootstrap/js/jquery-2.1.4.js"></script>
<script>
$(document).ready(function () {
window.resizeTo(10,70)
})
</script>
<script src="/static/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
// 调用的第三方编辑器,会自动把在id_content里面替换成编辑器,而这个id_content是form里面在渲染html页面的时候自动带出来的
CKEDITOR.replace('id_content')
</script>
{% endblock %}
后端代码如下:
def add_article(request):
'''
除了添加文章的方法
:param request:
:return:
'''
import datetime
errors = None
article_list = forms.get_form(models.Article)
if request.method == 'POST':
article_form = forms.get_form(models.Article,request.POST,request.FILES)
if article_form.is_valid():
data = article_form.cleaned_data
data['author_id'] = request.user.userprofile.id
if data['status'] == "published":
data['pub_date'] == datetime.datetime.now() article_obj = models.Article(**data)
article_obj.save()
return HttpResponseRedirect('/bbs/') else:
errors = '输入的信息不完整或者错误,不符合格式。' return render(request,'BBS/add_article.html',{'article':article_list,
'category_list':category_list,
'errors':errors,
'title_of_page':u'写一篇新文章'})
5 改查自己的文章(删除动作忘记写了)
前端代码:
# 其实还是调用之前新写一篇文章的的页面,我只是把内容给填充到里面去了,一举多得,哈哈。
<form class="form-horizontal" method="post" enctype="multipart/form-data">{% csrf_token %}
<div class="form-group">
// 遍历form的值。
{% for info in article %}
{% if info.field.required %}
<label style="font-size: larger">{{ info.label }}*</label></br>
{% else %}
{{ info.label }}</br>
{% endif %}
{{info}}</br>
{% endfor %}
<input name="author" style="display: none;" value="{{ request.user.userprofile.id }}">
</div>
<div class="col-sm-11">
<input type="submit" class="btn btn-primary " value="Save">
<a href="/person_zone/{{ request.user.userprofile.id }}" class="btn btn-danger">Cancel</a>
</div>
</form>
{% block bottom-js %}
<script src="/static/bootstrap/js/jquery-2.1.4.js"></script>
<script>
$(document).ready(function () {
window.resizeTo(10,70)
})
</script>
<script src="/static/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
// 调用的第三方编辑器,会自动把在id_content里面替换成编辑器,而这个id_content是form里面在渲染html页面的时候自动带出来的 CKEDITOR.replace('id_content')
</script>
{% endblock %}
后端代码:
def article_modify(request,article_id):
'''
更改文章/博客内容的方法
:param request:
:param article_id: 文章ID
:return: 网页
'''
article_id_obj = models.Article.objects.get(id=article_id)
# get_form实际是上我form里的一个装饰器,
form = forms.get_form(models.Article,instance=article_id_obj)
if request.method == 'POST':
form = forms.get_form(models.Article,request.POST,instance=article_id_obj)
if form.is_valid():
form.save()
return HttpResponseRedirect('/person_zone/%s'%(request.user.userprofile.id))
return render(request,'BBS/add_article.html',{'category_list':category_list,
'title_of_page':u'修改文章信息',
'article':form,})
6 在首页显示文章具体详情,通过我们在前端展现一个板块的时候,我们当前的办搬砖:
前端代码:
{% extends 'base.html' %}
{% load customer_tag %}
{% load for_bbs %}
{% block page-container %}
<div class="">
{% for article in article_list %}
{% csrf_token %}
<div class="article-box row">
<div class="article-head-img col-md-4">
<img class="article-head-img" src="/static/{{ article.head_img|truncate_url }}" >
</div>
<div class="article-brief col-md-8">
<a class="article-title" href="{% url 'article_detail' article.id %}">
{{ article.title }}<br/>
</a>
<div class="article-brief-info">
<span style="display: none;" article_id="{{ article.id }}"></span>
<span>{{ article.author.name }} </span>
<span>{{ article.pub_date }} </span>
<span>{% filter_comment article as comments %} </span>
<span class="glyphicon glyphicon-comment" aria-hidden="true"></span>
{{ comments.comment_count }}
<a href="" style='text-decoration:none;' onclick="click_praise(this)"
class="glyphicon glyphicon-thumbs-up" aria-hidden="true">
{{ comments.thumb_count }}</a>
</div>
<div class="article-brief-text">
<p>{{ article.brief }}</p>
</div>
</div>
</div>
<hr>
{% endfor %}
</div>
<nav>
<ul style="" class="pagination">
{% if article_list.has_previous %}
<li class=""><a href="?page={{ article_list.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">«</span></a></li>
{% endif %}
{% for page_num in article_list.paginator.page_range %}
{% guess_page article_list.number page_num %}
{% endfor %}
{% if article_list.has_next %}
<li class="">
<a href="?page={{ article_list.next_page_number }}" aria-label="Previous">
<span aria-hidden="true">»</span>
</a>
</li>
{% endif %}
</ul>
</nav>
<div class="wrap-right">
</div>
<div class="clear-both"></div>
{% endblock %}
{% block bottom-js %}
<script src="/static/bootstrap/js/jquery-2.1.4.js"></script>
<script>
//获取点赞数的函数
function Get_Praise(article_id,ele){
$.get("{% url "post_comment" %}",
{'id':article_id,'choice':2}, //紧跟的是参数
function (data) {
$(ele).html(' '+data)
}
)
}
//提交点赞请求
function click_praise(ele){
var article_id = $(ele).siblings().first().attr('article_id');
var GetCsrf = $("input[name='csrfmiddlewaretoken']").val();
// begin post
$.post("{% url 'post_comment' %}",
{
'comment_type':2,
'article':article_id,
'user':'{{ request.user.userprofile.id }}',
'csrfmiddlewaretoken': GetCsrf,
}, //end post
function(callback){
if(callback=='error'){
alert('点赞失败,可能是网络问题导致的')
}else if(callback=='limit'){
alert('您已经点赞过一次了,请勿重复点赞!!')
} else{
Get_Praise(article_id,ele)
}
}
)
}
</script>
{% endblock %}
后端代码:
def category(request,category_id=4):
'''
展示页面顶部模块的名字.
:param request:
:param category_id:
:return:
'''
category_obj = models.Category.objects.get(id=category_id)
if category_obj.position_index == 1: # 1 表示是首页
article_list = models.Article.objects.filter(status='published')
else:
article_list = models.Article.objects.filter(category_id=category_obj.id,status='published') article_list = paginate(request,article_list) # 分页方法处理下,使其返回前台是分页的!
return render(request,'bbs/index.html',{'category_list':category_list,
'category_obj':category_obj,
'article_list':article_list})
好了,上面的代码差不多就实现了基础的功能,具体的代码细节,可以看本人的github,哈哈,as python newber , you can pratice python+Django+js+jinja by make BBS!!
python小打小闹之简陋版BBS的更多相关文章
- Python写的简陋版一对一聊天工具,全双工
好该睡觉了,明天还要上班~~~直接上代码,后面再总结 import threading import os import socket def RecvProcess ( UDP_Socket, Lo ...
- flask 第七章 简陋版智能玩具 +MongoDB初识和基本操作
1.简陋版web智能玩具 FAQ.py文件 import os from aip import AipSpeech, AipNlp from uuid import uuid4 "" ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
- Python之路,Day4 - Python基础4 (new版)
Python之路,Day4 - Python基础4 (new版) 本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 ...
- python核心编程第二版笔记
python核心编程第二版笔记由网友提供:open168 python核心编程--笔记(很详细,建议收藏) 解释器options:1.1 –d 提供调试输出1.2 –O 生成优化的字节码(生成 ...
- [Python 学习]2.5版yield之学习心得 - limodou的学习记录 - limodou是一个程序员,他关心的焦点是Python, DocBook, Open Source …
[Python 学习]2.5版yield之学习心得 - limodou的学习记录 - limodou是一个程序员,他关心的焦点是Python, DocBook, Open Source - [Pyth ...
- 目前比较流行的Python科学计算发行版
经常有身边的学友问到用什么Python发行版比较好? 其实目前比较流行的Python科学计算发行版,主要有这么几个: Python(x,y) GUI基于PyQt,曾经是功能最全也是最强大的,而且是Wi ...
- 《OpenCV3 计算机视觉--Python语言实现 第二版》源代码及纠错
1.源代码下载地址 <OpenCV3 计算机视觉--Python语言实现 第二版>由我们翻译,英文书名<Learning OpenCV3 Computer Vision with P ...
- 利用Python进行数据分析(第二版)电子版书籍分享
资料下载地址: 链接:https://pan.baidu.com/s/1y1C0bJPkSn7Sv6Eq9G5_Ug 提取码:vscu <利用Python进行数据分析(第二版)>高清中文版 ...
随机推荐
- 你了解javascript中的function吗?(1)
上一篇文章中 介绍了function在javascirpt作为一等公民所担任的重要责任,在不同 的上下文中它担任着不同的角色,在对象内部它可以是函数,同时又能充当名字空间,不仅如此所有的functio ...
- java util包概述
util是utiliy的缩写,意为多用途的,工具性质的包这个包中主要存放了:集合类(如ArrayList,HashMap等),随机数产生类,属性文件读取类,定时器类等类.这些类极大方便了Java编程, ...
- matlab中的卷积——filter,conv之间的区别
%Matlab提供了计算线性卷积和两个多项式相乘的函数conv,语法格式w=conv(u,v),其中u和v分别是有限长度序列向量,w是u和v的卷积结果序列向量. %如果向量u和v的长度分别为N和M,则 ...
- 记一次在Linux上面启动部署在tomcat服务器的程序
前提:Linux系统已安装好jre环境 1.文件结构: 文件说明: 部署文件包含以下文件:1.apache-tomcat-7 程序运行的应用服务器tomcat包含: war包:apache-tomc ...
- 在eclipse下如何安装下载好的插件
我们下载到的插件,如果是一个jar格式的包,那么我们所需要做的事,就是 第一,新建一个名为plugins的文件夹, 第二,新建一个名为eclipse的文件夹,再将plugins复制进eclipse中, ...
- 品牌OEM信息导入工具(实测支持Win10)
OEM修改,定制专属LOGO. 免费下载:http://yunpan.cn/cmZuTYWLIGX6Q 访问密码 2da7 备用通道: http://pan.baidu.com ...
- 生产环境下案例 No space left on device (inode使用满的情况)
第一种情况: 问题: 如果想磁盘写入数据提示如下错误: No space left on device. 通过df -h查看磁盘空间,发现没满,请问可能原因是什么? 解答: 可能是inode数量被消耗 ...
- RabbitMQ集群、镜像部署配置
1 RABBITMQ简介及安装 RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python.Ruby..NET.Java.JMS.C.PHP.Act ...
- springMvc的第一个demo
1.下载jar包 http://repo.spring.io/libs-release-local/org/springframework/spring/4.2.3.RELEASE/ 2.下载源码 j ...
- iOS开发,音效的播放简单实现以及音效播放的简单封装
一.音效的播放简单实现 二.音效播放的封装 -- 封装思路:将生成的SystemSoundID存放到字典中,每次播放的时候从字典中取出对应的SystemSoundID,没有的话再创建 头文件中定义类方 ...