【问题标题】:Django: Best Way to Push Buttons Without Page Reloads?Django:在不重新加载页面的情况下按下按钮的最佳方式?
【发布时间】:2014-12-18 22:37:24
【问题描述】:

我正在使用 djangoratingsapp 创建一个包含“喜欢”和“不喜欢”按钮的系统,一切似乎都运行良好。但是,我在模板中使用了一种非常基本的按钮布局类型,并使用标准 POST 协议来注册按钮按下和输入评级。我对这种方法的主要问题是,每个页面上有很多 cmets 用户可以按下类似按钮,而我写这篇文章的方式会在每次按下按钮后重新加载页面并将用户带回页面顶部这在实践中对最终用户来说非常烦人。我想知道在不重新加载 Django 应用程序中的整个页面的情况下注册按钮的最简单方法是什么。我搜索并正在研究彗星。我想知道是否有更简单的方法,或者如果有人想给我一些提示,我将不胜感激。这不是一个花哨的网站,我唯一需要的就是在不重新加载页面的情况下按下按钮,不需要其他任何东西,比如实时聊天或实时状态更新等。提前致谢。

这是我用于模板中按钮的简单代码:

<form action="/myapp/r/{{pageid}}/" method="post">
 {% csrf_token %}
 <input type="submit" class="btn" value="+1" name="likebtn"/>
</form>

这里是相关按钮的views.py代码:

if (request.POST.get('likebtn')):
        votenum = instance.likerating.votes #get the number of total votes before casting a vote
        pagelikerating = instance.likerating.add(score=2, user=request.user, ip_address=request.META['REMOTE_ADDR']) #use the djangoratings app to add the rating
        if votenum != instance.likerating.votes: #check to see if the voting went through or got blocked from IP limit. If voting went through, the votenum variable will be 1 integer lower than the new queried total votes
            instance.likeratingcounter += 1 #this is a simple integerfield in the model that serves as a counter for number of likes
            instance.save() #save the addition of +1 on the counter 

【问题讨论】:

  • 这不是 Comet 的用途。这是简单的 Ajax。
  • 糟糕,谢谢。这就是为什么我很难过。我想我现在明白了。

标签: python django django-templates comet


【解决方案1】:

因此,由于丹尼尔的一些澄清,我得到了答案。谢谢丹尼尔!您可以为它们使用简单的 AJAX。这是一个很好的指南,它也完全适用于使用 django 教程的探戈按钮: http://www.tangowithdjango.com/book/chapters/ajax.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多