django POST数据不起作用

时间:2016-03-03 13:02:33

标签: python django post

我想点击/circle页面中的蓝色div,发送值" niloofar"到/final,输出必须是** niloofar **,但它不起作用。

我如何编辑它? 我想通过隐藏输入发送值。

views.py:

from django.shortcuts import render


def circle(request):
    return render(request, 'circle.html')


def final(request):
    matn = request.POST.get("title", "")
return render(request, 'final.html', {'matn': matn})

circle.html:

<html>
<head>
<style>
div {
    width:100px;
    height:100px;
    background:lightblue;
} 
</style>
</head>

<body>
<a href="/final">
    <div></div> 
    <input type="hidden" name="title" value="niloofar">
</a>

final.html:

** {{ matn }} **

urls.py:

from django.conf.urls import include, url
from secondv.views import circle, final

urlpatterns = [
    url(r'^circle/', circle),
    url(r'^final/', final),
]

2 个答案:

答案 0 :(得分:3)

您无法通过链接POST。将其更改为使用form代替:

<style>
.mysubmit {
    width:100px;
    height:100px;
    background:lightblue;
} 
</style>

<form action="/final" method="post">{% csrf_token %}
    <input type="hidden" name="title" value="niloofar">
    <input type="submit" class="mysubmit" value="">
</form>

也将您的观点更改为:

def final(request):
    matn = request.POST.get("title", "")
    return render(request, 'final.html', {'matn': matn})

答案 1 :(得分:3)

您正在使用<a>,这会发出'GET'请求。 要提交POST,您必须使用“

<form action='/final' method='post'>
    <input type="hidden" name="title" value="niloofar">
    <input type="submit" />
</form>