django ajax json的实例代码
作者:q493383189 发布时间:2023-01-15 00:56:18
标签:django,ajax,json
1. views.py
定义views视图函数,将数据存入字典。并用压缩为json格式,dumps,并return。
import json
def get_comments(request, article_id):
article_obj = models.Article.objects.get(id=article_id)
article_comments = article_obj.comment_set.select_related()
comment_dict = {}
for i in article_comments:
print('comments_id', i.id)
print('article_id', i.article_id)
print('parent_comment_id', i.parent_comment_id)
print('comment_type', i.comment_type)
print('user_id', i.user_id)
print('user_name', i.user.name)
print('comment', i.comment)
print('date', type(i.date))
print('date', time.strftime("%Y-%m-%d %H:%M:%S", i.date.timetuple()))
comment_dict[i.id] = [i.comment_type, i.comment, time.strftime("%Y-%m-%d %H:%M:%S", i.date.timetuple()), i.article_id, i.user_id, i.user.name, i.parent_comment_id]
comment_json = json.dumps(comment_dict)
return HttpResponse(comment_json)
2. article.html中编辑js jquery,接受json数据,并处理并添加到html中
<script>
function getComments() {
$.get("{% url 'get_comment' one_article.id %}", function(callback){
console.log(callback);
var obj = JSON.parse(callback);
console.log(this.comment_type);
for (var key in obj){
console.log(key);
console.log(obj[key])
}
}
function getCsrf() {
return $("input[name='csrfmiddlewaretoken']").val();
}
$(document).ready(function () {
$(".comment-box button").click(function () {
var comment_text = $('.comment-box textarea').val();
if (comment_text.trim().length < 5){
alert("评论不能少于5个字")
}else {
$.post(
"{% url 'post_comment' %}",
{
'comment_type':1,
article_id: "{{ one_article.id }}",
parent_comment_id:null,
'comment':comment_text.trim(),
'csrfmiddlewaretoken':getCsrf()
},
function (callback) {
console.log(callback);
if (callback == 'post-comment-success'){
alert('post-comment-success');
getComments();
}
}
)
}
})
})
</script>
来源:https://blog.csdn.net/q493383189/article/details/75305347
0
投稿
猜你喜欢
- 在使用python函数print()时,如下代码会出现输出无法显示的问题:分三次在一行输出 123print(1, end="&q
- MySQL数据库配置技巧用root用户启动远程服务一直是安全大忌,因为如果服务程序出现问题,远程攻击者极有可能获得主机的完全控制权。MySQ
- 一、问题由来工作的局域网中,会接入很多设备,机器人上的网络设备就2个了,一个巨哥红外,一个海康可见光。机器人还有自身的ip。有时候机器人挂的
- Notebook 修改字体和大小原理很简单,就是更改CSS文件原本的字体很难看,尤其是 引号😡我推荐两款字体,Consolas 和 Fira
- 一、开发接口的作用1、mock接口:模拟一些接口。有一些有关联的接口,在别的接口没有开发好的时候,需要用这个接口,就可以写一个假接口,返回想
- 数组使用简介原文地址:30 Days of Mootools 1.2 Tutorials - Day 3 - Intro to Using
- 引言随着圣诞的到来,大家纷纷@官方微信给自己的头像加上一顶圣诞帽。当然这种事情用很多P图软件都可以做到。但是作为一个学习图像处理的技术人,还
- django rest framework使用django-filter注意事项:一定要在setting文件里面加载如下代码,而不是只安装包
- 在程序中,经常需要将⼀组(通常是同为某个类型的)数据元素作为整体 管理和使⽤,需要创建这种元素组,⽤变量记录它们,传进传出函数等。 ⼀组数据
- 连续看到几个和 Oracle 优化器隐含参数 _sort_elimination_cost_ratio 相关的优化案例(Refer Refe
- 尝试安装server[justin@xen20-vm04 "]$ rpm -ivh MySQL-server-5.1.51-1.g
- matplotlib默认根据数据系列自动缩放坐标轴范围。pyplot模块中的autoscale函数可以切换是否自动缩放坐标轴范围,xlim(
- 网上有这样一道题目:一个字符串String=“adadfdfseffserfefsefseetsdg”,找出里面出现次数最多的字母和出现的次
- 导言(Introduction)这个提案描述了如何在jQuery的核心库中增加模板支持。更为特别是,这个提案描述了一个新的jQuery方法-
- 在使用Python时,需要使用各种各样的库,通常会使用pip直接安装,这样最为简单也最方便。但最为崩溃的地方在于有时候速度出奇的慢,因为
- 本文实例为大家分享了python生成圆形图片的具体代码,供大家参考,具体内容如下# -*- coding: utf-8 -*- "
- 面对不断成长的用户,跟随用户的脚步齐步向前,做引起共鸣的改变,去除低龄化的设计,用成熟稳重的心态面对用户。QQBanner自2006 年推出
- IIS上设置301 跳转相信大家都会,只要在网站-属性-主目录里 选择重定向URL就行了,这样整站就跳转到目标站点了,但是有个问
- 作者:Dmitry @ Usability Post 版权所有 Copyright.译者:明月星光 @ UCD翻译小组原文:ht
- 使用摄像头追踪人脸由于血液流动引起的面部色素的微小变化实现实时脉搏评估。效果如下(演示视频): 由于这是通过比较面部色素的变化评估