niloofar
niloofar

Reputation: 2334

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.

How I can edit it? I want to send the value by hidden input.

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),
]

Upvotes: 1

Views: 3302

Answers (2)

Selcuk
Selcuk

Reputation: 59425

You can't POST from a link. Change it to use a form instead:

<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})

Upvotes: 3

ilse2005
ilse2005

Reputation: 11439

You are using <a> and this will issue a 'GET' request. To submit a POST you have to use a `

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

Upvotes: 3

Related Questions