Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot create new Django model object within Ajax post request
    primarykey
    data
    text
    <p>This is kind of "I already lost x hours debugging this" kind of problem/question :(</p> <p>Following jQuery js code is initiating POST request upon button click</p> <pre><code>$("#btn_create_tag").click(function(evt) { $.post("/tag/createAjax", { tagname: $("#txt_tag_name").val() }, function(data) { } ); }); </code></pre> <p>Django code that is performed on this call is:</p> <pre><code>@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 </code></pre> <p>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.</p> <p>When I execute </p> <pre><code>new_tag = Tag() new_tag.name = tagName new_tag.save() </code></pre> <p>from Django shell, new Tag object is regulary created, but from jQuery request, it's not created.</p> <p>Have any idea what's wront, what to check, how to debug this....</p> <p>DB behind is PostgresSQL 8.3.</p> <p>Any suggestion is more then welcome :)</p> <p><strong>Update</strong>:</p> <p>I wrote UnitTest which is working:</p> <pre><code>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) </code></pre> <p><strong>Update2</strong>:</p> <p>Hum, this morning, it seems that everything is working fine, but code hasn't changed. I don't like this at all :(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload