Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想要一个upvote按钮,当点击它时,它会增加数据库中一个字段(整数)的值_Python_Html_Django_Web Applications_Django Templates - Fatal编程技术网

Python 我想要一个upvote按钮,当点击它时,它会增加数据库中一个字段(整数)的值

Python 我想要一个upvote按钮,当点击它时,它会增加数据库中一个字段(整数)的值,python,html,django,web-applications,django-templates,Python,Html,Django,Web Applications,Django Templates,我的按钮的html是 <td> <a href="{% url 'minor:upvote' %}"> <button type="button" class="likes" style="text-align:center;border-radius:40px;width: 100px;height: 40px">upvote</button> </a> </td> 观点是 def upv(reques

我的按钮的html是

<td>
  <a href="{% url 'minor:upvote'  %}">
    <button type="button" class="likes" style="text-align:center;border-radius:40px;width: 100px;height: 40px">upvote</button>
  </a>
</td>
观点是

def upv(request,id):
    reporter = Logg.objects.get(id=id)
    reporter.upvote = reporter.upvote+1
    reporter.save()
    return redirect('/')

但upvote字段(即默认值为0的整数字段)没有递增

如果您没有将
Logg
ID传递给视图,请向URL添加一个数字参数,并将ID传递给
URL
templatetag:

URL路由:

path(“(?P\d+”,view=views.upv,name='upvote'),
模板:

{% url 'minor:upvote' my_current_logg.pk %}

您必须在
urlpatterns
定义中传递id。由于您使用的是
path
,因此要将id添加到视图参数中,请使用以下命令

path('<id>/', views.upv, name="upvote")

我认为它与id有关,在“reporter.upvote=reporter.upvote+1”这行前后打印upvote。检查这行是否正常工作,但是先生,html模板怎么办,它也必须被编辑。谢谢,现在它正常工作了,我在模板中也添加了Logg.id。酷,很高兴我能帮助你酷,如果它解决了你的问题,请接受答案
path('<id>/', views.upv, name="upvote")
<td>
  <a href="{% url 'minor:upvote' id=logg.id  %}">
    <button type="button" class="likes" style="text-align:center;border-radius:40px;width: 100px;height: 40px">upvote</button>
  </a>
</td>