Skip to content Skip to sidebar Skip to footer

Cannot Create New Django Model Object Within Ajax Post Request

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

Solution 1:

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.

Solution 2:

If you are using the TransactionMiddleware (see http://docs.djangoproject.com/en/dev/topics/db/transactions/), then not returning a HttpResponse of some sort will result in a crash and a rollback.

Solution 3:

try

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

BTW - you should sanitize the new tagname and not take it directly from the POST

Post a Comment for "Cannot Create New Django Model Object Within Ajax Post Request"