I think I'm doing something wrong...

1 view
Skip to first unread message

Elver Loho

unread,
Jun 6, 2006, 10:00:29 PM6/6/06
to django...@googlegroups.com
...because Django can't be this horribly broken. I'm sure of that
much. But I've been beating my head against the wall for quite some
time now and worst of all, I've got a deadline. So here's me, on the
mailing list.

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

Malcolm Tredinnick

unread,
Jun 6, 2006, 10:29:53 PM6/6/06
to django...@googlegroups.com
On Wed, 2006-06-07 at 05:00 +0300, Elver Loho wrote:
[...]

> 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


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


Adrian Holovaty

unread,
Jun 6, 2006, 10:31:39 PM6/6/06
to django...@googlegroups.com
On 6/6/06, Elver Loho <elver...@gmail.com> wrote:
> Anyhow, the code.
>
>
> URL:
> (r'^make/$', 'latestbooks.latest.views.make_sidebar'),
>
> HTML:
> <form action="/make" method="POST">

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

Zanchey

unread,
Jun 6, 2006, 10:31:56 PM6/6/06
to Django users
Elver,

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

Zanchey

unread,
Jun 6, 2006, 10:32:47 PM6/6/06
to Django users
Haha, three replies within two minutes. Sorry for the spam.

David Adam
zan...@gmail.com

Max Battcher

unread,
Jun 6, 2006, 10:37:48 PM6/6/06
to django...@googlegroups.com
Elver Loho wrote:
> <form action="/make" method="POST">

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)

Elver Loho

unread,
Jun 6, 2006, 10:47:12 PM6/6/06
to django...@googlegroups.com
Darn, well, that fixed it :P

And to think I had the same problem with TurboGears a while bak. Sheesh.

Thanks, everyone! :)


Elver

Reply all
Reply to author
Forward
0 new messages