【问题标题】:django POST data not workingdjango POST数据不起作用
【发布时间】:2016-03-03 13:02:33
【问题描述】:

我想当我点击/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),
]

【问题讨论】:

  • 那么你在哪里定义message什么“不起作用”意味着什么?错误?结果无效?
  • 我已经编辑了我的问题。
  • 没有错误只是值不会出现,只是开始。

标签: python django post


【解决方案1】:

您不能通过链接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})

【讨论】:

  • 你绝对可以从一个链接发帖,虽然你需要ajax:&lt;a href="#" onmousedown='$.ajax(config)'&gt;&lt;/a&gt;。您的答案可能是首选和优越的解决方案,但说您不能从链接发布是完全不正确的。如果您愿意,您可以从任何 HTML 元素发布,但它是否是一个好主意是完全不同的故事......(在大多数情况下使用您所说的表单,这将是 stupid 使用链接,但您可以使用链接。)
  • @YungGun 您的示例不是从 链接 发布的。那就是使用&lt;a&gt; 标签发布。链接与任何可点击的文本都不同。通过这种逻辑,您还可以从&lt;blink&gt; 标签发布。您可以使用链接获取,但不能使用 POST。
【解决方案2】:

您正在使用&lt;a&gt;,这将发出一个“GET”请求。 要提交POST,您必须使用`

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-30
    • 2012-02-18
    • 2019-01-26
    • 1970-01-01
    • 2012-12-29
    • 2018-03-06
    • 2015-09-01
    • 2016-04-17
    相关资源
    最近更新 更多