在Ajax发布请求中无法创建新的Django模型对象

编程入门 行业动态 更新时间:2024-10-10 11:29:51
本文介绍了在Ajax发布请求中无法创建新的Django模型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是一种我已经丢失x小时调试这个的问题/问题:(

以下jQuery js代码正在启动POST请求按钮点击

$(#btn_create_tag)点击(function(evt){ $ .post(/ tag / createAjax,{标记名:$(#txt_tag_name)。val()},函数(数据){} ); });

此呼叫执行的Django代码为:

@suppress_logging_output @login_required def createAjax(request):如果request.is_ajax()和request.method = ='POST': tagName = request.POST [tagname] new_tag = Tag() new_tag.name = tagName new_tag.save()打印带有%s的新标签已创建%new_tag.id

该代码已成功执行(我正在检查空的或已经存在的名字,但没有写她e要更清晰),但不会创建新的Tag对象。 我甚至得到了在devserver提示符下打印出的新标签,其中已经创建了id%s,并且每次增加ID为一个,但是对象不存储在db中。

当我执行

new_tag = Tag() new_tag。来自Django shell的name = tagName new_tag.save()

新的Tag对象是Regulary创建,但从jQuery请求,它不是创建。

有什么想法,什么检查,如何调试这个....

DB后面是PostgresSQL 8.3。

任何建议都欢迎:)

更新:

我写了UnitTest,它正在工作:

class AjaxTestCase(TestCase): def testAjaxCreateTag(self): tagNum = Tag.objects.filter(name =TEST_TAG)count() self.assertEqual(tagNum,0)c = Client() c.post('/ lookup / tag / creat eAjax',{'tagname':'TEST_TAG'},HTTP_X_REQUESTED_WITH ='XMLHttpRequest') tagNum = Tag.objects.filter(name =TEST_TAG)count() self.assertEqual(tagNum ,1)

Update2 :

$嗯,今天早上,似乎一切都正常,但代码没有改变。我不喜欢这个:(

解决方案

这听起来很奇怪,你可以仔细检查你的数据库设置吗?你在 settings.py 中使用正确的数据库?还要写一个单元测试来使用Django的测试客户端在您的测试方法中,请记住发送 HTTP_X_REQUESTED_WITH 头为 is_ajax()工作。

This is kind of "I already lost x hours debugging this" kind of problem/question :(

Following jQuery js code is initiating POST request upon button click

$("#btn_create_tag").click(function(evt) { $.post("/tag/createAjax", { tagname: $("#txt_tag_name").val() }, function(data) { } ); });

Django code that is performed on this call is:

@suppress_logging_output @login_required def createAjax(request): if request.is_ajax() and request.method == 'POST': tagName = request.POST["tagname"] new_tag = Tag() new_tag.name = tagName new_tag.save() print "new tag with id %s has been created" % new_tag.id

That code is executed successfully (I'm doing checks for empty or already existing name, but didn't wrote here to be more clear), but new Tag object is NOT created. I'm even getting ""new tag with id %s has been created" printed on devserver's prompt, and every time ID is increased for one, as suppossed to, but objects are not stored in db.

When I execute

new_tag = Tag() new_tag.name = tagName new_tag.save()

from Django shell, new Tag object is regulary created, but from jQuery request, it's not created.

Have any idea what's wront, what to check, how to debug this....

DB behind is PostgresSQL 8.3.

Any suggestion is more then welcome :)

Update:

I wrote UnitTest which is working:

class AjaxTestCase(TestCase): def testAjaxCreateTag(self): tagNum = Tag.objects.filter(name="TEST_TAG").count() self.assertEqual(tagNum, 0) c = Client() c.post('/lookup/tag/createAjax', {'tagname': 'TEST_TAG'}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') tagNum = Tag.objects.filter(name="TEST_TAG").count() self.assertEqual(tagNum, 1)

Update2:

Hum, this morning, it seems that everything is working fine, but code hasn't changed. I don't like this at all :(

解决方案

This sounds very strange. Can you double check your database settings? Ensure that you are using the correct database inside settings.py? Also write an unit test to exercise the code using Django's test client. In your test method remember to send the HTTP_X_REQUESTED_WITH header for is_ajax() to work.

更多推荐

在Ajax发布请求中无法创建新的Django模型对象

本文发布于:2023-10-10 22:10:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   对象   Ajax   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!