views:

from django.shortcuts import HttpResponse,redirect,render
from app01.models import *
import json # 使用json格式数据
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
from back.views.ddff import defense_url,ddff_list @defense_url
def add(request,aid=None):
'''管理员添加'''
res = {'s':None,'info':None} # 提交
print(request.POST)
if request.method == 'POST':
# 标题
if request.POST.get('article_title') == '':
res['s']= 1
res['info'] = '标题不能为空'
return HttpResponse(json.dumps(res))
article_title = request.POST.get('article_title').strip() # 标题查询
c_article_obj = Article.objects.filter(article_title=article_title).first()
print(c_article_obj)
# 找不到
if c_article_obj is None:
res['info'] = '标题查询不到'
res['title'] = str(Article.objects.filter(article_title__icontains=article_title).first())
print(res['title'])
res['t'] = ''
if res['title'] is None:
res['t'] = ''
return HttpResponse(json.dumps(res))
# 用户id
# if request.POST.get('memberid') == '':
# res['info'] = '用户不能为空'
# return HttpResponse(json.dumps(res))
member_id = request.POST.get('memberid')[0].strip() # 时间
if request.POST.get('time') == '':
res['info'] = '时间不能为空'
return HttpResponse(json.dumps(res))
time1 = request.POST.get('time').strip() # 内容
if request.POST.get('content') == '':
res['s'] = 1
res['info'] = '内容不能为空'
return HttpResponse(json.dumps(res))
content = request.POST.get('content').strip() # 是否评论修改
comment_id = request.POST.get('commentid').strip()
if comment_id == '':
comment_id = 0
# 评论id是否存在
mm = Comment.objects.filter(id=comment_id) if mm == '':
# 修改
print(mm)
assert print(1)
mm[0].update(
comment_content=content,comment_addtime=time1,article_id=c_article_obj,member_id=member_id)
res['s'] = 1
res['info'] = '用户修改成功!'
return HttpResponse(json.dumps(res))
else:
# 新建品论
a_obj = Comment(comment_content=content,comment_addtime=time1,article=c_article_obj,member_id=member_id)
a_obj.save()
res['s'] = 1
res['info'] = '添加成功!'
return HttpResponse(json.dumps(res)) comment_obj = Comment.objects.filter(id=aid).first()
members = Member.objects.all()
articles = Article.objects.all()
return render(request,'comment/add.html',locals()) @ddff_list
def list(request):
'''manage 列表''' comment = Comment.objects.all()
members = Member.objects.all()
return render(request,'comment/list.html',locals()) def list_part(request):
'''ajax分页器'''
where = getWhere(request)
comment_list = Comment.objects.filter(**where).all()
currentPage = int(request.GET.get('page', 1))
paginator = Paginator(comment_list, 5)
if paginator.num_pages > 6:
if currentPage - 2 < 1:
pageRange = range(1, 6)
elif currentPage + 2 > paginator.num_pages:
pageRange = range(currentPage - 2, paginator.num_pages + 1)
else:
pageRange = range(currentPage - 2, currentPage + 2)
else:
pageRange = paginator.page_range
try:
comment_list = paginator.page(currentPage)
except PageNotAnInteger:
comment_list = paginator.page(1)
except EmptyPage:
comment_list = paginator.page(paginator.num_pages)
return render(request, 'comment/list_part.html', locals()) def getWhere(request)->dict:
'''where拼接'''
where = {}
title = request.POST.get('title')
aid = request.POST.get('aid')
article_clicknum = request.POST.get('article_clicknum')
member_id = request.POST.get('member_id')
if title:
where['article__article_title__icontains'] = title
if article_clicknum:
if article_clicknum == '':
where['article__article_clicknum__gt'] = 1000
elif article_clicknum == '':
where['article__article_clicknum__gt'] = 500
elif article_clicknum == '':
where['article__article_clicknum__gt'] = 0
if member_id:
where['member_id'] = member_id
if aid:
where['id'] = aid
print(where)
return where def delete(request):
'''删'''
if request.method == 'POST':
id = request.POST.get('id')
else:
id = request.GET.get('id')
result = Comment.objects.filter(id=id).delete()
import json
res = {'status': None, 'info': None}
if result:
res['status'] = 1
res['info'] = '操作成功'
else:
res['status'] = 0
res['info'] = '操作失败'
return HttpResponse(json.dumps(res)) # 把这个结果告诉给前台,ajax def change(request):
'''改'''
if request.method == 'POST':
aid = request.POST.get('id')
else:
aid = request.GET.get('id')
comment = Comment.objects.filter(id=aid).first()
print(comment)
return add(request,aid) @defense_url
def comment(request):
'''品论'''
if request.method == 'POST':
aid = request.POST.get('id')
else:
aid = request.GET.get('id')
comments = Comment.objects.filter(article_id=aid).all()
article_obj = Article.objects.filter(id=aid).first()
print('评论')
return render(request, 'comment/list.html', locals())

html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>列表页</title>
<link href="/static/back/css/style.css" rel="stylesheet" type="text/css" />
<link href="/static/back/css/select.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.css">
<script type="text/javascript" src="/static/back/js/jquery.js"></script>
<script type="text/javascript" src="/static/back/js/jquery.idTabs.min.js"></script>
<script type="text/javascript" src="/static/back/js/select-ui.min.js"></script>
<script type="text/javascript" src="/static/layer/layer.js"></script> <script type="text/javascript">
$(document).ready(function(e) {
$(".select1").uedSelect({
width : 345
});
$(".select2").uedSelect({
width : 167
});
$(".select3").uedSelect({
width : 100
});
});
</script> </head> <body>
<div class="place">
<span>位置:</span>
<ul class="placeul">
<li><a href="{% url 'back:index/index/' %}">首页</a></li>
<li><a href="{% url 'back:index/index/' %}">系统设置</a></li>
</ul>
</div> <div class="formbody">
<div id="usual1" class="usual">
<div id="tab2" class="tabson"> {# form表单 #}
<form >
{% csrf_token %}
{% if aid %}
<input type="hidden" name="aid" value="{{ aid }}">
{% else %}
<ul class="seachform"> <li>
<label>文章标题查询</label><input name="title" type="text" class="scinput" />
</li> <li><label>作者</label>
<div class="vocation">
<select class="select3" name="member_id">
<option value="">全部</option>
{% for i1 in members %}
<option value="{{ i1.id }}">{{ i1.member_name }}</option>
{% endfor %}
</select>
</div>
</li> {# <li><label>点击量</label>#}
{# <div class="vocation">#}
{# <select class="select3" name="article_clicknum">#}
{# <option value="">全部</option>#}
{# <option value="3">热度</option>#}
{# <option value="2">普通</option>#}
{# <option value="1">少量</option>#}
{# </select>#}
{# </div>#}
{# </li>#} <li>
<label>&nbsp;</label><input name="" type="button" class="scbtn" value="查询" id="search"/>
</li> </ul>
{% endif %}
</form>
<table class="tablelist">
<thead>
<tr>
<th><input name="" type="checkbox" value="" checked="checked"/></th>
<th>编号<i class="sort"><img src="/static/back/images/px.gif" /></i></th>
<th>评论</th>
<th>发布时间</th>
<th>文章</th>
<th>用户</th>
<th>操作</th>
</tr>
</thead>
<tbody> <tbody class="maina"> </tbody> </tbody>
</table> </div> </div>
<script> //首次页面加载,通过ajax拿到数据,放在maina容器里
show_list();
function show_list() {
var url = '{% url 'back:comment/list_part/' %}';
$.post(url, $('form').serialize(), function (data) {
$(".maina").html(data);
});
}
//点击分页,点击a连接:
$(document).on('click', '.pagin a', function () {
$.post(this.href, $('form').serialize(), function (data) {
$(".maina").html(data);
});
return false;//让a连接失效
}); //点击查看
$(document).on('click', '.change', function () {
var id = $(this).parent().data("id");
location.href="/back/comment/change/?id="+id;
{# $.post("{% url 'back:article/delete/' %}", {#}
{# "id": id,#}
{# "csrfmiddlewaretoken": "{{ csrf_token }}"#}
{# }, function (data) {#}
{##}
{# })#}
}); //点击品论
$(document).on('click', '.comment', function () {
var id = $(this).parent().data("id");
location.href="/back/comment/comment/?id="+id;
{# $.post("{% url 'back:article/delete/' %}", {#}
{# "id": id,#}
{# "csrfmiddlewaretoken": "{{ csrf_token }}"#}
{# }, function (data) {#}
{##}
{# })#}
}); //点击删除
$(document).on('click', '.del', function () {
_this = this;
layer.confirm('您确定要删除吗?', {
btn: ['确定', '取消']
}, function () {
id = $(_this).parent().data("id");
$.post("{% url 'back:comment/delete/' %}", {
"id": id,
"csrfmiddlewaretoken": "{{ csrf_token }}"
}, function (data) {
if (data.status == 1) {
layer.msg(data.info, function () {
$(_this).parent().parent().remove();
}); } else {
layer.msg(data.info);
}
}, 'json');
}, function () {
});
return false;//让a连接失效
}); //点击查询按钮
$(document).on('click', '#search', function () {
show_list();
}); </script> <script type="text/javascript">
$("#usual1 ul").idTabs();
</script> <script type="text/javascript">
$('.tablelist tbody tr:odd').addClass('odd');
</script> </div> </body> </html>

html_list_part:

{% for i in comment_list %}
<tr>
<td><input name="" type="checkbox" value="" /></td>
<td>{{ forloop.counter }}</td>
<td>{{ i.comment_content }}</td>
<td>{{ i.comment_addtime }}</td>
<td>{{ i.article }}</td>
<td>{{ i.member }}</td>
<td data-id="{{ i.id }}">
<a href="javascript:;" class="tablelink change">修改</a>
<a href="javascript:;" class="tablelink del"> 删除</a></td>
</tr>
{% endfor %}
<tr class="pagin"><th colspan="7"> {# <div class="pagin">#}
<span class="message " align="right">共 <i class="blue">{{ paginator.count }} </i> 条 记 录 </span>
{# <ul class="paginList">#}
{##}
{# </ul>#}
{# </div>#} {#分页器#}
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-end">
{% if paginator.has_previous %}
<li class="page-item disabled">
<a class="page-link" href="/back/comment/list_part/?page={{ p.previous_page_number }}" tabindex="-1" aria-disabled="true">上一页</a>
</li>
{% endif %}
{% for i in pageRange %} <li class="page-item {% if i == currentPage %}current{% endif %}"><a class="page-link" href="/back/comment/list_part/?page={{ i }}">{{ i }}<span class="sr-only">(current)</span></a></li>
{% endfor %} {% if paginator.has_next %}
<li class="page-item">
<a class="page-link" href="/back/comment/list_part/?page={{ p.next_page_number }}">下一页</a>
</li>
{% endif %}
</ul>
</nav>
</th></tr>

python-Web-django-ajax分页的更多相关文章

  1. python web -- django

    一. 安装 django $ pip install django (env)$ python >> import django >> django.VERSION >& ...

  2. python web——Django架构

    环境:windows/linux/OS 需要的软件:Firefox 浏览器(别的也可以 不过firfox和python的webdriver兼容性好) git版本控制系统(使用前要配置 用户 编辑器可以 ...

  3. Django - Ajax分页

    目前总结了2种方法: 1. Ajax 分页 尼玛各种google,stackoverflow,搞了好久才总结出这个,之前使用Pagination tag loading的方式不好用,并且不能进行aja ...

  4. python学习-- Django Ajax CSRF 认证

    使用 jQuery 的 ajax 或者 post 之前 加入这个 js 代码:http://www.ziqiangxuetang.com/media/django/csrf.js /*======== ...

  5. python web django base skill

    web框架本质 socket + 业务逻辑 框架实现socket tonado node.js 使用WSGI实现socket django flask 自己实现框架思路 wsgiref socket ...

  6. python web django 2nd level -- 待更新

    练习代码位置 实例代码位置 --> app: myblog Form 利用Form表单验证,自己写的html 思路: 新建一个类 LoginForm(forms.Form) 新建对象 obj = ...

  7. python三大web框架Django,Flask,Flask,Python几种主流框架,13个Python web框架比较,2018年Python web五大主流框架

    Python几种主流框架 从GitHub中整理出的15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python We ...

  8. Django,Flask,Tornado三大框架对比,Python几种主流框架,13个Python web框架比较,2018年Python web五大主流框架

    Django 与 Tornado 各自的优缺点Django优点: 大和全(重量级框架)自带orm,template,view 需要的功能也可以去找第三方的app注重高效开发全自动化的管理后台(只需要使 ...

  9. python web框架——扩展Django&tornado

    一 Django自定义分页 目的:自定义分页功能,并把它写成模块(注意其中涉及到的python基础知识) models.py文件 # Create your models here. class Us ...

  10. python web框架 Django进阶

    django 进阶 基础中,一些操作都是手动创建连接的非主流操作,这样显得太low,当然也是为了熟悉这个框架! 实际中,django自带连接数据库和创建app的机制,同时还有更完善的路由系统机制.既然 ...

随机推荐

  1. java poi 生成多个sheet

    我的需求是:在一个表格中生成多个sheet,每个sheet的名称动态指定,每个sheet内的内容动态指定.生成的文件名动态指定. 工具类: package test; import java.io.O ...

  2. Linux的SSH免密登录(一)

    1.从cp/scp命令出发 scp(secure copy)是linux系统下基于ssh登录进行安全的远程文件拷贝的命令. 1. 传递文件到远程 scp local_file remote_usern ...

  3. 字符串操作-格式化输出-Python

    字符串操作示例 去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = ...

  4. 简单的c程序分析

    1.裸函数 c语言的普通函数中即使什么都不写,编译器在编译时也会给它加上一些汇编代码:比如开栈.返回等: 裸函数就是编译器什么都不管,一切都需要自己来处理: 裸函数的优点是自由度高,可以应用在钩子程序 ...

  5. tomcat下载与安装

    https://www.cnblogs.com/limn/p/9358657.html

  6. UBUNTU 15.10 CAFFE安装教程(测试可用)

    转帖:https://github.com/BVLC/caffe/wiki/Ubuntu-15.10-Installation-Guide Ubuntu 15.10 have been release ...

  7. 灰度图像--图像分割 Sobel算子

    学习DIP第44天 转载请标明本文出处:http://blog.csdn.net/tonyshengtan,欢迎大家转载,发现博客被某些论坛转载后,图像无法正常显示,无法正常表达本人观点,对此表示很不 ...

  8. Anadi and Domino

    C - Anadi and Domino 参考:Anadi and Domino 思路:分为两种情况: ①n<=6,这个时候肯定可以保证降所有的边都放上一张多米诺牌,那么答案就是m ②n==7, ...

  9. CSS效果篇--这里有你想要的CSS3漂亮的自定义Checkbox各种复选框

    在原来有一篇文章写到了<CSS效果篇--纯CSS+HTML实现checkbox的思路与实例>.这篇文章主要写各种自定义的checkbox复选框,实现如图所示的复选框: 大致的html代码都 ...

  10. Java实现字串统计

    对字符串的操作,无论再难的算法题,只要时间充足,相信每个同学都可以搞定. 但是浪费太多时间去搞一个逻辑算法没太大意义,学会学习,不但可以增长自己的知识,更可以节省时间,俗话说,一寸光阴一寸金,寸金难买 ...