Django 增量视图 + 1
我不知道为什么这次我无法显示项目的“浏览次数”,我以前这样做过,但在这个项目中我不能
def getAuto(request,marca,slug,id):
from django.db.models import F
object = get_object_or_404(Robado,marca__slug=marca,modelo__slug=slug,pk=id,publico=True)
object.views= F('views')+1
object.save()
template.html
views: {{object.views}}
template.html 显示:
(+: (DEFAULT: ), 1)
我无法想象为什么
谢谢你们
i dont know why this time i cant show the "number of views" of a items, i was doing this before but in this project i cant
def getAuto(request,marca,slug,id):
from django.db.models import F
object = get_object_or_404(Robado,marca__slug=marca,modelo__slug=slug,pk=id,publico=True)
object.views= F('views')+1
object.save()
template.html
views: {{object.views}}
The template.html is showing:
(+: (DEFAULT: ), 1)
i cant imagine why
Thanks you guys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不能像这样使用
F()
。它用于filter
表达式,您可以在其中将一个字段与同一模型上的另一个字段进行比较,也可用于update
表达式,您可以在其中增加一个字段place:但我不明白你为什么要在这里使用它,因为你已经拥有了该对象。直接引用属性会更容易:
或者更简单:
I don't think you can use
F()
like this. It's for use infilter
expressions, where you can use it to compare one field with another field on the same model, and inupdate
expressions, where you can increment a field in place:but I don't see why you're trying to use it here, where you already have the object. It's easier just to reference the attribute directly:
or even more simply: