无法从Ajax视图创建对象

时间:2010-11-30 13:08:48

标签: jquery django django-templates

编辑:问题解决了,现在我又遇到了另一个问题。当应该返回“OK”或“EXISTS”时,数据变量不返回任何内容。

我有一个带叠加效果的模板。 income.html模板有一个表单和一个“添加新类别”按钮,当您单击它时,将显示一个带有小窗体的新窗口(叠加效果)。我不知道为什么,但这个类别没有被创建。看来我在html脚本中遇到了一些错误,但我无法识别它们。

income.html:

(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);
    }
}))  
});
</script>
...

<form>{% csrf_token %}
       <label for="name">Name:</label><input type="text" id="nc" />
       <input type="submit" value="Submit" id="new_cat" />
</form>

views.py:

@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)

2 个答案:

答案 0 :(得分:2)

这看起来有点狡猾:

{ name: cat_name, account: {{ account }} }

请记住,这是在Javascript块中呈现的。 cat_name是一个JS变量,因此将被正确评估。但{{ account }}呈现为字符串,因此就JS而言,它是一个文字。所以它需要在引号中:

{ name: cat_name, account: "{{ account }}" }

答案 1 :(得分:1)

在new_cat视图中(我认为tipo等于类型)。

除此之外,我猜错误就在这里:

account = request.GET[u'account']
c = Category.objects.filter(name=name, account=account)

帐户是一个unicode字符串,但您需要一个帐户实例来过滤类别。这样的事应该可以正常工作:

account_st = request.GET[u'account']
account = Account.objects.get(name=account_st)
c = Category.objects.filter(name=name, account=account)