为什么我的Django视图在每个页面视图中都被击中两次? [英] Why is my Django view being hit twice with every page view?

查看:64
本文介绍了为什么我的Django视图在每个页面视图中都被击中两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到我生命中的问题。很简单,我有一个要从数据库中提取的数据库对象,将其视图增加一个,然后保存。我的视图显示的是增加的值,但是随后我的日志显示该值再次增加。

I can't seem to find the problem for the life of me. Very simply, I have a database object that I'm pulling from the database, incrementing it's "views" by one, and saving. My view display's the incremented value, but then my logs show that the value is incremented AGAIN.

g=Game.objects.filter(slug=slug).distinct()[0]
g.views += 1
g.save()

这是我的日志:

[Fri Oct 29 15:15:49 2010] [error] DEBUG:root:Updating plays
[Fri Oct 29 15:15:49 2010] [error] DEBUG:root:plays: 40
[Fri Oct 29 15:15:50 2010] [error] DEBUG:root:Updating plays
[Fri Oct 29 15:15:50 2010] [error] DEBUG:root:plays: 41

我的视图显示它具有40个匹配。这会导致我的视​​图每次刷新时都会增加2:

My view shows that it has 40 hits. This causes that my view increments by 2 every time I refresh:

[Fri Oct 29 15:20:19 2010] [error] DEBUG:root:Updating plays 
[Fri Oct 29 15:20:19 2010] [error] DEBUG:root:plays: 42
[Fri Oct 29 15:20:19 2010] [error] DEBUG:root:Updating plays
[Fri Oct 29 15:20:19 2010] [error] DEBUG:root:plays: 43

关于这可能是什么的任何提示?

Any hints as to what this could be?

编辑:

这是我的观点。我将其简化为核心元素(并且仍然表现异常)。

Here's my view. I simplified it to the core elements (and it still behaves oddly).

def game(request, slug=None):

    g=Game.objects.filter(slug=slug)[0]

    if g:
        comments=GameComment.objects.filter(item=g, parent__isnull=True)
        g.plays+=1
        g.save()
    else:
        comments=None

    return render_to_response('goto/goto_game.html', {'g': g, 'comments':comments, 'cCount':len(comments) if comments else 0, 'newCCount':0}, request=request)


推荐答案

AH!原来这是缺少的url和ajax问题。这不是网站图标,因为它只是根源,而不是我的特定观点。

AH! Turns out it was a missing url and ajax issue. It wasn't the favicon, as that only hits the root, and not my specific view.

问题出在我有这样的AJAX命令时:

The problem came when I had an AJAX command like this:

$.post('{{url("_submitcomment")}}', data, function(data) { ...


$.post(''); 

将访问您当前的URL,并尝试获取所有内容!

will hit your current URL and try to fetch everything!

(我使用的是Jinja2,btw,因此url命令可能看起来与传统的Django模板系统不同。)

(I'm using Jinja2, btw, so the url command probably looks different from them traditional Django template system.)

感谢您的帮助和提示!

这篇关于为什么我的Django视图在每个页面视图中都被击中两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆