简体   繁体   English

django POST数据不起作用

[英]django POST data not working

I want when I click the blue div in /circle page, send the value "niloofar" to /final and the output must be ** niloofar ** , but it doesnt work. 我想要当我在/circle页面中单击蓝色div时,将值“ niloofar”发送到/final ,并且输出必须是** niloofar ** ,但是它不起作用。

How I can edit it? 我该如何编辑? I want to send the value by hidden input. 我想通过隐藏输入发送值。

views.py: 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: 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: final.html:

** {{ matn }} **

urls.py: urls.py:

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

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

You can't POST from a link. 你不能POST从一个链接。 Change it to use a form instead: 将其更改为使用form

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

and

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

also change your view to: 还将您的视图更改为:

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

You are using <a> and this will issue a 'GET' request. 您正在使用<a> ,这将发出“ GET”请求。 To submit a POST you have to use a ` 要提交POST您必须使用`

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM