Django 转储 JSON 数据
我正在尝试输入一个单词并通过ajax将其显示在页面上。我缺少一些简单的东西...
所以我用 Jquery 发送这样的信息:
$.ajax({
url: url,
type:"POST",
data:{'word': word},
success: function(data){
//do something
}
});
并且该信息正在进入视图并保存到数据库中。当我尝试返回新单词时,问题发生了:
def add_word(request, lecture_id):
l = get_object_or_404(Lecture, pk=lecture_id)
if request.method == "POST":
#see if there is a value with p
if request.POST.has_key('word') and request.POST['word'] != "":
success = {}
try:
oldWord = l.post_set.get(word=request.POST['word'])
except:
newWord = l.post_set.create(word=request.POST['word'], count = 1)
success = {'new': str(newWord.word), 'count': str(newWord.count)}
else:
oldWord.count += 1
oldWord.save()
success = {'old': str(oldWord.word), 'count': str(oldWord.count)}
return HttpResponse(json.dumps(success), mimetype="application/javascript")
return HttpResponse(reverse('post.views.lecture_display', args=(l.id,)))
我收到 500 错误...
[13/Oct/2011 15:14:48] "POST /lecture/3/add HTTP/1.1" 500 66975
I'm trying to enter a word and have it show up on page through ajax. There something simple I'm missing...
So I'm sending the info like this with Jquery:
$.ajax({
url: url,
type:"POST",
data:{'word': word},
success: function(data){
//do something
}
});
and the information is getting into the view and saving into the DB. The problem happens when I try to return the new word:
def add_word(request, lecture_id):
l = get_object_or_404(Lecture, pk=lecture_id)
if request.method == "POST":
#see if there is a value with p
if request.POST.has_key('word') and request.POST['word'] != "":
success = {}
try:
oldWord = l.post_set.get(word=request.POST['word'])
except:
newWord = l.post_set.create(word=request.POST['word'], count = 1)
success = {'new': str(newWord.word), 'count': str(newWord.count)}
else:
oldWord.count += 1
oldWord.save()
success = {'old': str(oldWord.word), 'count': str(oldWord.count)}
return HttpResponse(json.dumps(success), mimetype="application/javascript")
return HttpResponse(reverse('post.views.lecture_display', args=(l.id,)))
Im getting a 500 error...
[13/Oct/2011 15:14:48] "POST /lecture/3/add HTTP/1.1" 500 66975
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有看到回溯,我的猜测是失败的是[其中之一]:
直接在浏览器中打开 URL,您将获得默认的 Django 回溯,500 视图。
Without seeing the traceback, my guess is that what's failing is [one of]:
Open the URL directly in your browser, and you'll get the default Django traceback, 500 view.
我认为你需要学习调试而不是特定的修复。
如果问题仍然存在,请使用
ipdb
或pudb
包,在视图中插入以下行并分析代码中发生的情况:def myview(请求, id):
导入ipdb; ipdb.set_trace()
使用 Chrome 开发者工具或 Firebug 查看服务器输出的内容以及打开的 URL。另请查看 Django 调试工具栏 和 Werkzeug。调试工具栏可以显示所有已渲染的模板和所有局部变量。 Werkzeug 还可以直接从浏览器在调用堆栈的任何位置为您提供调试 shell。
I think you need to learn debugging rather that a particular fix.
If the problem persists, use
ipdb
orpudb
package, insert the following line in the view and analyze what happens inside your code:def myview(request, id):
import ipdb; ipdb.set_trace()
Use Chrome Developer Tools or Firebug to see what the server outputs and what urls it opens. Also take a look at Django Debug Toolbar and Werkzeug. The debug toolbar can show you all the templates that were rendered and all the local variables. Werkzeug also gives you a debug shell in any place of the call stack right from the browser.