What I'm trying to accomplish here is a simple POST form that posts to
the same view. Caching is turned off. I'm not using Django's template
system, but Kid. However, this shouldn't be a problem. As for the
version, then I did an svn update about five minutes ago, while this
same problem has also been present on the version from last night and
on the version from about two weeks ago.
Anyhow, the code.
URL:
(r'^make/$', 'latestbooks.latest.views.make_sidebar'),
HTML:
<form action="/make" method="POST">
<select name="days" size="1">
<option selected="1" value="0">Ainult samal paeval ilmunud raamatud</option>
<option value="1">Täna ja eile ilmunud raamatud</option>
<option value="2">Täna, eile ja üleeile ilmunud raamatud</option>
</select>
<input type="text" name="foo" value="bar" />
<input type="submit" value="Edasi" />
</form>
VIEW:
def make_sidebar(request):
print "POST.keys()", request.POST.keys()
print "GET.keys()", request.GET.keys()
RESULT:
[07/Jun/2006 04:50:23] "POST /make HTTP/1.1" 301 0
POST.keys() []
GET.keys() []
[07/Jun/2006 04:50:23] "GET /make/ HTTP/1.1" 200 1063
[07/Jun/2006 04:50:23] "GET /media/latestbooks.css HTTP/1.1" 304 0
I have another view working where the request.GET dictionary gets
constructed properly when I pass the query string in the URL itself.
(e.g., /?foo=bar&and=etc) So I don't think it's problems with any
settings as such.
The problem might be in my form. However, the XHTML looks valid, from
what I can tell.
This is driving me nuts, and I don't mean in the funny pirate joke sort of way.
Help?
Elver
One thing I would suggest is to change your form action to submit to
"/make/". The trailing slash will avoid the redirect you are seeing (the
301 response code). Note the warning in the HTTP/1.1 spec about
redirects from POSTs here:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2
It looks like your POST is being resubmitted as a GET and the form
values may well be being dropped in the process (unless the redirected
URL shows them as converted to GET params).
Malcolm
Your form is pointing at /make, but your view is at /make/ (note the
trailing slash). Django's CommonMiddleware is automatically adding the
slash (according to the APPEND_SLASH setting), and POST data isn't
transferred in redirects. Point the form action at /make/ (with the
trailing slash) and your problem should be solved.
Adrian
--
Adrian Holovaty
holovaty.com | djangoproject.com
I haven't had much of a play with it, but I get much better results
from your little test program if I add a trailing slash to the action
field of your form.
i.e.
- <form action="/make" method="POST">
+ <form action="/make/" method="POST">
This avoids the redirect that Django sends when it receives a URL
without a trailing slash, trashing your form information.
David Adam
zan...@gmail.com
David Adam
zan...@gmail.com
Sadly it seems you, along with so many, many others, have fallen into
the easiest problem in the book. Your action address should have a
following /.
http://code.djangoproject.com/wiki/NewbieMistakes#POSTtoviewslosesPOSTdata
--
--Max Battcher--
http://www.worldmaker.net/
"I'm gonna win, trust in me / I have come to save this world / and in
the end I'll get the grrrl!" --Machinae Supremacy, Hero (Promo Track)
And to think I had the same problem with TurboGears a while bak. Sheesh.
Thanks, everyone! :)
Elver