{"id":2326,"date":"2022-08-30T15:23:56","date_gmt":"2022-08-30T15:23:56","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/05\/cannot-create-new-django-model-object-within-ajax-post-request-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:23:56","modified_gmt":"2022-08-30T15:23:56","slug":"cannot-create-new-django-model-object-within-ajax-post-request-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/cannot-create-new-django-model-object-within-ajax-post-request-collection-of-common-programming-errors\/","title":{"rendered":"Cannot create new Django model object within Ajax post request-Collection of common programming errors"},"content":{"rendered":"<p>This is kind of &#8220;I already lost x hours debugging this&#8221; kind of problem\/question \ud83d\ude41<\/p>\n<p>Following jQuery js code is initiating POST request upon button click<\/p>\n<pre><code>$(\"#btn_create_tag\").click(function(evt) {\n$.post(\"\/tag\/createAjax\", { \n    tagname: $(\"#txt_tag_name\").val()       \n    },\n    function(data) {\n    }\n);\n});\n<\/code><\/pre>\n<p>Django code that is performed on this call is:<\/p>\n<pre><code>@suppress_logging_output\n@login_required\ndef createAjax(request):\n    if request.is_ajax() and request.method == 'POST':\n        tagName = request.POST[\"tagname\"]\n        new_tag = Tag()\n        new_tag.name = tagName\n        new_tag.save()\n        print \"new tag with id %s has been created\" % new_tag.id\n<\/code><\/pre>\n<p>That code is executed successfully (I&#8217;m doing checks for empty or already existing name, but didn&#8217;t wrote here to be more clear), but new Tag object is NOT created. I&#8217;m even getting &#8220;&#8221;new tag with id %s has been created&#8221; printed on devserver&#8217;s prompt, and every time ID is increased for one, as suppossed to, but objects are not stored in db.<\/p>\n<p>When I execute<\/p>\n<pre><code>new_tag = Tag()\nnew_tag.name = tagName\nnew_tag.save()\n<\/code><\/pre>\n<p>from Django shell, new Tag object is regulary created, but from jQuery request, it&#8217;s not created.<\/p>\n<p>Have any idea what&#8217;s wront, what to check, how to debug this&#8230;.<\/p>\n<p>DB behind is PostgresSQL 8.3.<\/p>\n<p>Any suggestion is more then welcome \ud83d\ude42<\/p>\n<p><strong>Update<\/strong>:<\/p>\n<p>I wrote UnitTest which is working:<\/p>\n<pre><code>class AjaxTestCase(TestCase):\n    def testAjaxCreateTag(self):\n        tagNum = Tag.objects.filter(name=\"TEST_TAG\").count()\n        self.assertEqual(tagNum, 0)\n        c = Client()\n        c.post('\/lookup\/tag\/createAjax', {'tagname': 'TEST_TAG'}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')\n        tagNum = Tag.objects.filter(name=\"TEST_TAG\").count()\n        self.assertEqual(tagNum, 1)\n<\/code><\/pre>\n<p><strong>Update2<\/strong>:<\/p>\n<p>Hum, this morning, it seems that everything is working fine, but code hasn&#8217;t changed. I don&#8217;t like this at all \ud83d\ude41<\/p>\n<ol>\n<li>\n<p>This sounds very strange. Can you double check your database settings? Ensure that you are using the correct database inside <code>settings.py<\/code>? Also write an unit test to exercise the code using Django&#8217;s test client. In your test method remember to send the <code>HTTP_X_REQUESTED_WITH<\/code> header for <code>is_ajax()<\/code> to work.<\/p>\n<\/li>\n<li>\n<p>try<\/p>\n<pre><code>new_tag=Tag(name=tagName)\nnew_tag.save()\n<\/code><\/pre>\n<p>BTW &#8211; you should sanitize the new tagname and not take it directly from the POST<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-05 10:10:33. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>This is kind of &#8220;I already lost x hours debugging this&#8221; kind of problem\/question \ud83d\ude41 Following jQuery js code is initiating POST request upon button click $(&#8220;#btn_create_tag&#8221;).click(function(evt) { $.post(&#8220;\/tag\/createAjax&#8221;, { tagname: $(&#8220;#txt_tag_name&#8221;).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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2326","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2326"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2326\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}