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. 集合(六) WeakHashMap与LinkedHashMap

    5.WeakHashMap (1) 简介 WeakHashMap与HashMap几乎都是相同的,就是它的键是“弱引用”. 第一个问题:何为弱引用?即WeakReference类对象. String a ...

  2. 关于上部盒子里有图片下面盒子magin-top负值的层级问题

    .box{ width: 800px; box-sizing: border-box; margin: 50px auto 0; background: pink; } .imgBox{ width: ...

  3. 箭头函数中可改变this作用域,回调函数用箭头函数this指向page,自定义事件用箭头函数this指向undefined

    1.回调函数中,用箭头函数改变this的作用域 success: (res)=>{ this.setData({ //此时,this指向page页面 ... }) } 2.自定义事件中,如果使用 ...

  4. php类相关知识---__unset和__isset

    __unset 删除非公有属性,在外部调用unset时发生,  __isset用来检测对象属性是否设置值 <?php class coach { protected $chairfit = &q ...

  5. js实现移动端悬浮图标拖拽

    /** * Created by Administrator on 2019/5/23. */ window.onload = function () { var oDiv = document.ge ...

  6. JVM(一),谈谈你对java的理解

    一.谈谈你对java的理解 1.Java特性 (1)平台无关性 一次编译到处运行 (2)GC 垃圾回收机制 (3)语言特性 泛型-反射机制-lambda表达式 (4)面向对象 面向对象语言-三大特性( ...

  7. 关于数据库表设计之区域表system_district:省市县街道四级地址表

    关于省市县的数据表的设计有两种方式: 一.将其设计成一张表 DROP TABLE IF EXISTS `system_district`; CREATE TABLE `system_district` ...

  8. mybatis oracle mysql 批量插入时的坑爹问题--需谨记

    mybatis oracle mysql 批量插入一.oracle的批量插入方式insert into db(id, zgbh, shbzh) select '1', '2', '3' from du ...

  9. 【CSS】三栏/两栏宽高自适应布局大全

    页面布局 注意方案多样性.各自原理.各自优缺点.如果不定高呢.兼容性如何 三栏自适应布局,左右两侧300px,中间宽度自适应 (1) 给出5种方案 方案一: float (左右浮动,中间不用给宽,设置 ...

  10. springboot 2.2.0 SNAPSHOT 解决 repositories.repository.id must be unique 的问题

    如果打包 jar 也报错了 ,那也是这个的原因,注释掉即可 <!-- 这个仓库必须注释掉 否则打包 war 的时候 , 会报错 'repositories.repository.id' must ...