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进行数据分析(第二版)>高清中文版 ...
随机推荐
- MC700 安装双系统
2011年买的MBP MC700给老婆用了一段时间后,老婆还不习惯不了Mac OS或是虚拟机,要求必须给安装windows,无奈时隔四年后,只能重新尝试在MC700上用bootcamp安装Window ...
- AngularJS引入Echarts的Demo
最近要用到图表展示,想了想,还是首选Echarts,HighCharts和D3.js备用吧, 而项目中也用到了AngularJS,所以需要把Echarts引入到AngularJs中一起使用, 试了试, ...
- CSS3属性border-radius绘制多种多样的图形
border-radius,国内翻译成圆角,你可能以为这个属性就是用来画圆角,没错,但是除此之外,它还可以做点别的事情.radius其实指的是边框所在圆的半径,这个CSS3属性不仅能够创建圆角,还可以 ...
- 【SPOJ 8222】Substrings
http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
- hadoop,mapreduce---分布式计算
从图中可以看出,map阶段的shuffle: 例如word count,当内存缓冲区满的时候会写到磁盘,一个spill,每个spill,进行分区,排序,最后将同一个分区word合并在一起,写入到磁盘中 ...
- java基础语法要点<一>(基于1.8)
http://yishouce.com/java/run http://www.shucunwang.com/RunCode/java/ 数据类型 8种基本数据类型及对应的 类型封装器 byte, s ...
- js-函数eval
eval函数接收一个参数s,如果s不是字符串,则直接返回s.否则执行s语句.如果s语句执行结果是一个值,则返回此值,否则返回undefined. 需要特别注意的是对象声明语法“{}”并不能返回一个值, ...
- Mybatis 高级结果映射 ResultMap Association Collection
在阅读本文章时,先说几个mybatis中容易混淆的地方: 1. mybatis中的列不是数据库里的列而是查询里的列,可以是别名(如 select user_name as userName,这时col ...
- GameObject.Instantiate(游戏体的实例化),角色的选择
using UnityEngine; using System.Collections; public class CharacterCreation : MonoBehaviour { public ...
- SQL查询记录是否在另一个表中存在
1.需求 create table ta(id int);create table tb(id int);insert into ta values(1);insert into ta values( ...