Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't create an object from an Ajax view
    text
    copied!<p><strong>Edit</strong>: The problem was solved, now I've another problem. The data variable returns nothing when it should return "OK" or "EXISTS". </p> <p>I have a template with an overlay effect. The income.html template has a form and a button "Add a new category", when you click on it a new window is displayed(overlay effect) with a tiny form. I don't know why, but the category doesn't get created. It seems I've got some errors in the html script but I can't identify them. </p> <p>income.html:</p> <pre><code>(document).ready(function(){ $("#new_cat").live("click", ( function() { var cat_name = $("#nc").val(); if (cat_name) { $.get("/cashflow/new_cat/0/", { name: cat_name, account: {{ account }} }, function(data){ if (data == "OK") { $("#id_category").val(cat_name); } if (data == "EXISTS") { var error = "The category already exists"; alert(error); } }); } else { var error = "Please enter a name"; alert(error); } })) }); &lt;/script&gt; ... &lt;form&gt;{% csrf_token %} &lt;label for="name"&gt;Name:&lt;/label&gt;&lt;input type="text" id="nc" /&gt; &lt;input type="submit" value="Submit" id="new_cat" /&gt; &lt;/form&gt; </code></pre> <p>views.py:</p> <pre><code>@login_required def income(request, account_name): account = account_name if request.method == 'POST': form = TransForm(user=request.user, data=request.POST) if form.is_valid(): income = form.save(commit=False) income.type = 0 income.account = Account.objects.get( name = account_name, user = request.user) income.name = form.cleaned_data['name'] income.category = form.cleaned_data['category'] income.save() uri = ("/cashflow/account/%s") % str(account_name) return HttpResponseRedirect(uri) else: form = TransForm(user=request.user) context = { 'TransForm': form, 'type': '0', 'account': account, } return render_to_response( 'cashflow/income.html', context, context_instance = RequestContext(request), ) def new_cat(request, tipo): if request.method == u'GET': GET = request.GET if GET.has_key(u'name'): name = request.GET[u'name'] account = request.GET[u'account'] c = Category.objects.filter(namee=name, account=account) if c: s = "EXISTS" else: c = Category(name = name, user = request.user, type = type, account = account) c.save() s = "OK" return HttpResponse(s) </code></pre>
 

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