【发布时间】:2010-12-22 17:42:41
【问题描述】:
当数据变量应该返回“OK”或“EXISTS”时,它什么也不返回。
我有一个带有叠加效果的模板。 income.html 模板有一个表单和一个“添加新类别”按钮,当您单击它时,会显示一个带有小表单的新窗口(叠加效果)。
收入.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, type):
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(name=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)
编辑:调试信息
> ../cashflow/views.py(765)new_cat()-><django....xa507b0c>
-> return HttpResponse(message)
(Pdb) p message
'EXISTS'
(Pdb) n
> ../site-packages/django/core/handlers/base.py(112)get_response()
-> if response is None:
p response.content
'EXISTS'
(Pdb) p response.status_code
200
奇怪的是,当我想调试时,覆盖显示在调试时,而不是之后。这就是为什么我认为这是一个 JS 错误。我忘了告诉类别已创建并正确保存到数据库中。
【问题讨论】: