将视图与django中的html按钮关联
我有一个按钮和计数器。按一下按钮,计数器按1加1。 查看代码:将视图与django中的html按钮关联,django,Django,我有一个按钮和计数器。按一下按钮,计数器按1加1。 查看代码: def details(request,id): post=Post.objects.get(pk=id) if request.POST['my_button_on_html']: post.counter +=1 post.save() return render(request,'details.html',{'post':post}) 和html: {{post.counter}} <input ty
def details(request,id):
post=Post.objects.get(pk=id)
if request.POST['my_button_on_html']:
post.counter +=1
post.save()
return render(request,'details.html',{'post':post})
和html:
{{post.counter}}
<input type="submit" name="my_button_on_html" />
<form action="#" method="post">
{%csrf_token%}
<input type="submit" value="{{post.product_like}}" name="button">
</form>
{{post.counter}
我的问题是“如何说查看:当我点击按钮时做一些事情”
你怎么知道的
html:
<form action="" method="post">
{% csrf_token %}
{{post.product_like}}
<input type="submit" name="like" value='{{post.product_id}}' />
</form>
{%csrf_令牌%}
{{post.product_like}}
您可以将按钮连接到一个视图,该视图添加一个按钮,并使用按钮的onclick属性返回相同的页面
<button onclick.action={% url site:counter %} >Click me</button >
但是,每次单击时都会重新加载整个页面,所以我很确定您不会希望这样做 好的,我自己做的: 观点:
def details(request,id):
post=Post.objects.get(pk=id)
if(request.POST.get('button')):
post.product_like +=1
post.save()
return render(request,'details.html',{'post':post})
和html:
{{post.counter}}
<input type="submit" name="my_button_on_html" />
<form action="#" method="post">
{%csrf_token%}
<input type="submit" value="{{post.product_like}}" name="button">
</form>
{%csrf_令牌%}
当您单击“提交”时,它当前在做什么?那个按钮在
中吗?提交是增加我的计数器的按钮。是的。你能发布你的完整html表格吗?{%csrf\u token%}{{post.product\u like}}您在模板中使用了不同的名称,如果请求,可以查看…if request.post['like']:
和post.product\u like+=1
no-isnt:D我的问题是:计数器是投票或类似键。怎么增加呢?