Django 转储 JSON 数据

发布于 2024-12-09 11:23:52 字数 1248 浏览 1 评论 0原文

我正在尝试输入一个单词并通过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城已无爱 2024-12-16 11:23:52

如果没有看到回溯,我的猜测是失败的是[其中之一]:

# A) This path is not resolving correctly (see named-URLs in Django's docs)
reverse('post.views.lecture_display', args=(l.id,))

# B) This word has unicode data, which can't simply be passed to ``str``
str(oldWord.word)

直接在浏览器中打开 URL,您将获得默认的 Django 回溯,500 视图。

Without seeing the traceback, my guess is that what's failing is [one of]:

# A) This path is not resolving correctly (see named-URLs in Django's docs)
reverse('post.views.lecture_display', args=(l.id,))

# B) This word has unicode data, which can't simply be passed to ``str``
str(oldWord.word)

Open the URL directly in your browser, and you'll get the default Django traceback, 500 view.

伴梦长久 2024-12-16 11:23:52

我认为你需要学习调试而不是特定的修复。

  1. 尝试在没有发布数据的情况下打开该网址,看看是否存在语法或名称错误。
  2. 如果问题仍然存在,请使用 ipdbpudb 包,在视图中插入以下行并分析代码中发生的情况:

    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.

  1. Try opening that url without post data, see if there's a syntax or a name error.
  2. If the problem persists, use ipdb or pudb 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文