request.method not working as expected

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

request.method not working as expected

Fabio Natali-2
Hi everybody!

I have a very simple view which is supposed to print out GET or POST,
depending which has been used to send data. The problem is I always get
"GET" even when sending data via POST.

### views.py:
def method(request):
     return HttpResponse(request.method)

### urls.py:
(r'^method/$', method)

### php script to send data to the Django page:
<form method="post" action="http://192.168.0.2:8000/method/">
   <input type="text" id="firstname">
   <input type="submit" value="Send">
</form>

I am running Django version 1.2.5, development server.

Am I missing something? Any hint?

Thanks, Fabio.

--
Fabio Natali FNstudio

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: request.method not working as expected

DrBloodmoney
On Tue, Sep 20, 2011 at 11:46 AM, Fabio Natali <[hidden email]> wrote:

> Hi everybody!
>
> I have a very simple view which is supposed to print out GET or POST,
> depending which has been used to send data. The problem is I always get
> "GET" even when sending data via POST.
>
> ### views.py:
> def method(request):
>    return HttpResponse(request.method)
>
> ### urls.py:
> (r'^method/$', method)
>
> ### php script to send data to the Django page:
> <form method="post" action="http://192.168.0.2:8000/method/">
>  <input type="text" id="firstname">
>  <input type="submit" value="Send">
> </form>
>
> I am running Django version 1.2.5, development server.
>
> Am I missing something? Any hint?
>
> Thanks, Fabio.
>
> --
> Fabio Natali FNstudio

I'm not 100% sure, but since you aren't including the
csrfmiddlewaretoken in the post data, I would expect it to 403 if you
aren't exempting the view from CSRF protection [1].

[1] https://docs.djangoproject.com/en/1.3/ref/contrib/csrf/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Testcases are not working with PREPEND_WWW

Felix Stiehler-4
Hello there, I am trying to use the Django way of testing, but it
somehow does not work together with the PREPEND_WWW setting. All I get
is the 301 status code and no redirect is followed even when follow=True.

from django.test.client import Client
c = Client()
r = c.get('/users/login/', follow=True)
r.status_code
301
r.content
''
r.redirect_chain
[]

Any help how to make the tests work without touching some webserver
config stuff would be appreciated.

Felix




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: request.method not working as expected

Fabio Natali-2
In reply to this post by DrBloodmoney
On 09/20/2011 05:53 PM, DrBloodmoney wrote:

> On Tue, Sep 20, 2011 at 11:46 AM, Fabio Natali<[hidden email]>  wrote:
>> Hi everybody!
>>
>> I have a very simple view which is supposed to print out GET or POST,
>> depending which has been used to send data. The problem is I always get
>> "GET" even when sending data via POST.
>>
>> ### views.py:
>> def method(request):
>>     return HttpResponse(request.method)
>>
>> ### urls.py:
>> (r'^method/$', method)
>>
>> ### php script to send data to the Django page:
>> <form method="post" action="http://192.168.0.2:8000/method/">
>>   <input type="text" id="firstname">
>>   <input type="submit" value="Send">
>> </form>
>>
>> I am running Django version 1.2.5, development server.
>>
>> Am I missing something? Any hint?
>>
>> Thanks, Fabio.
>>
>> --
>> Fabio Natali FNstudio
>
> I'm not 100% sure, but since you aren't including the
> csrfmiddlewaretoken in the post data, I would expect it to 403 if you
> aren't exempting the view from CSRF protection [1].
>
> [1] https://docs.djangoproject.com/en/1.3/ref/contrib/csrf/

Hi DrBloodmoney, thanks for your kind reply.

I've modified my view as follows:

### views.py:
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def method(request):
      return HttpResponse(request.method)

However everything behaves the same way, I still have "GET" erroneously
detected.

Do you have any other hint on how to investigate?

Thanks, Fabio.

--
Fabio Natali FNstudio

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: request.method not working as expected

Babatunde Akinyanmi
In reply to this post by Fabio Natali-2
Correct me if I'm wrong but it seems your "method" function is
supposed to return the request method type(POST or GET) sent to the
view. If I'm correct then your syntax will always return GET. That's
because I think all the django middleware workings adjust the initial
arguments and the response such that it always ends up as a GET.
Try storing request.method in a variable first and then return that
variable with your HttpResponse like

def method(request):
    method_type = request.method
    Return HttpResponse(method_type)

Haven't tried it but I'm sure this will give the desired result

On 9/20/11, Fabio Natali <[hidden email]> wrote:

> Hi everybody!
>
> I have a very simple view which is supposed to print out GET or POST,
> depending which has been used to send data. The problem is I always get
> "GET" even when sending data via POST.
>
> ### views.py:
> def method(request):
>      return HttpResponse(request.method)
>
> ### urls.py:
> (r'^method/$', method)
>
> ### php script to send data to the Django page:
> <form method="post" action="http://192.168.0.2:8000/method/">
>    <input type="text" id="firstname">
>    <input type="submit" value="Send">
> </form>
>
> I am running Django version 1.2.5, development server.
>
> Am I missing something? Any hint?
>
> Thanks, Fabio.
>
> --
> Fabio Natali FNstudio
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to
> [hidden email].
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

--
Sent from my mobile device

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: request.method not working as expected

dm03514
In reply to this post by Fabio Natali-2
Fabio in your dev server output is it saying the type of request is
get?
ARe you accessing "/method/" through your form or are you just
directing your webserver to http://192.168.0.2:8000/method/???


On Sep 20, 11:46 am, Fabio Natali <[hidden email]> wrote:

> Hi everybody!
>
> I have a very simple view which is supposed to print out GET or POST,
> depending which has been used to send data. The problem is I always get
> "GET" even when sending data via POST.
>
> ### views.py:
> def method(request):
>      return HttpResponse(request.method)
>
> ### urls.py:
> (r'^method/$', method)
>
> ### php script to send data to the Django page:
> <form method="post" action="http://192.168.0.2:8000/method/">
>    <input type="text" id="firstname">
>    <input type="submit" value="Send">
> </form>
>
> I am running Django version 1.2.5, development server.
>
> Am I missing something? Any hint?
>
> Thanks, Fabio.
>
> --
> Fabio Natali FNstudio

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: request.method not working as expected

Fabio Natali-2
In reply to this post by Babatunde Akinyanmi
On 09/20/2011 07:12 PM, Babatunde Akinyanmi wrote:

> Correct me if I'm wrong but it seems your "method" function is
> supposed to return the request method type(POST or GET) sent to the
> view. If I'm correct then your syntax will always return GET. That's
> because I think all the django middleware workings adjust the initial
> arguments and the response such that it always ends up as a GET.
> Try storing request.method in a variable first and then return that
> variable with your HttpResponse like
>
> def method(request):
>      method_type = request.method
>      Return HttpResponse(method_type)

Dear Babatunde, I had already tried that with no luck, that didn't work.

Thank you very much for your suggestion and your kind reply.

--
Fabio Natali FNstudio

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: request.method not working as expected

Fabio Natali-2
In reply to this post by dm03514
On 09/20/2011 07:15 PM, dm03514 wrote:
> Fabio in your dev server output is it saying the type of request is
> get?
> ARe you accessing "/method/" through your form or are you just
> directing your webserver to http://192.168.0.2:8000/method/???

Hi dm03514 and thanks for your help!

Thanks to your suggestion I managed to resolve my issue. I looked at my
development server logs and noticed 2 lines:

[20/Sep/2011 12:17:27] "POST /method HTTP/1.1" 301 0
[20/Sep/2011 12:17:27] "GET /method/ HTTP/1.1" 200 3

I guess the lack of a trailing slash in my url was causing some sort of
internal redirection. That redirection eventually caused the loss of
POST stuff.

I was using this:
<form method="post" action="http://192.168.0.2:8000/method">
I changed that to:
<form method="post" action="http://192.168.0.2:8000/method/">
and everything was fixed.

Don't you think this is too fragile and error prone? I guess I may
forget the trailing slash again in the future and that will mean the
break of everything... Is there any best practice for this?

Thanks! Greetings, Fabio.

--
Fabio Natali FNstudio

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: request.method not working as expected

Andres Reyes
I believe that the best practice is to always use the {% url %} template tag and not hardcoding your URL's

2011/9/20 Fabio Natali <[hidden email]>
On 09/20/2011 07:15 PM, dm03514 wrote:
Fabio in your dev server output is it saying the type of request is
get?
ARe you accessing "/method/" through your form or are you just
directing your webserver to http://192.168.0.2:8000/method/???

Hi dm03514 and thanks for your help!

Thanks to your suggestion I managed to resolve my issue. I looked at my development server logs and noticed 2 lines:

[20/Sep/2011 12:17:27] "POST /method HTTP/1.1" 301 0
[20/Sep/2011 12:17:27] "GET /method/ HTTP/1.1" 200 3

I guess the lack of a trailing slash in my url was causing some sort of internal redirection. That redirection eventually caused the loss of POST stuff.

I was using this:

<form method="post" action="http://192.168.0.2:8000/method">
I changed that to:

<form method="post" action="http://192.168.0.2:8000/method/">
and everything was fixed.

Don't you think this is too fragile and error prone? I guess I may forget the trailing slash again in the future and that will mean the break of everything... Is there any best practice for this?

Thanks! Greetings, Fabio.


--
Fabio Natali FNstudio

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Andrés Reyes Monge
[hidden email]
+(505)-8873-7217

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: request.method not working as expected

Fabio Natali-2
On 09/21/2011 07:57 AM, Andres Reyes wrote:
> I believe that the best practice is to always use the {% url %} template
> tag and not hardcoding your URL's

That makes perfect sense. Thanks for your precious tip!

So I added {% url add_to_cart %} to my template. However, it silenty
fails and I am redirected to my main page with no error whatsoever.

### urls.py
(r'^add_to_cart/$', add_to_cart)

Any tips?

Thanks and greetings, Fabio.

--
Fabio Natali FNstudio

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: request.method not working as expected

Babatunde Akinyanmi
In reply to this post by Fabio Natali-2
Your post showed the trailing url...anyway I agree with your
diagnosis. With my noob knowledge of django, django tries to add a
trailing slash to your url when you don't. I believe she does that by
adding the trailing slash to the url and then asking the server to
redirect to the url which is the reason why the development server
logs show 301 status. However, when a 301 is gotten in response to a
request and the request method wasn't GET or HEAD, the client is
expected to ask the user before redirecting. If there is no automatic
addition of the trailing slash, the server will return a 500 instead
because django would be able to find the url in your given pattern and
raise an error.
My guess is that maybe django explicitly does the redirection by
sending a GET. I guess this is one of the times we need to check the
guts of django to understand her more.
However, if you believe you simply won't be able to cope with the
trailing slash, you could change your url pattern to exclude the
trailing slash though I don't know if that has side effects.....that's
if you must absolutely hard code the url

On 9/21/11, Fabio Natali <[hidden email]> wrote:

> On 09/20/2011 07:15 PM, dm03514 wrote:
>> Fabio in your dev server output is it saying the type of request is
>> get?
>> ARe you accessing "/method/" through your form or are you just
>> directing your webserver to http://192.168.0.2:8000/method/???
>
> Hi dm03514 and thanks for your help!
>
> Thanks to your suggestion I managed to resolve my issue. I looked at my
> development server logs and noticed 2 lines:
>
> [20/Sep/2011 12:17:27] "POST /method HTTP/1.1" 301 0
> [20/Sep/2011 12:17:27] "GET /method/ HTTP/1.1" 200 3
>
> I guess the lack of a trailing slash in my url was causing some sort of
> internal redirection. That redirection eventually caused the loss of
> POST stuff.
>
> I was using this:
> <form method="post" action="http://192.168.0.2:8000/method">
> I changed that to:
> <form method="post" action="http://192.168.0.2:8000/method/">
> and everything was fixed.
>
> Don't you think this is too fragile and error prone? I guess I may
> forget the trailing slash again in the future and that will mean the
> break of everything... Is there any best practice for this?
>
> Thanks! Greetings, Fabio.
>
> --
> Fabio Natali FNstudio
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to
> [hidden email].
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

--
Sent from my mobile device

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: request.method not working as expected

Babatunde Akinyanmi
In reply to this post by Fabio Natali-2
What's in your add_to_cart view function?

On 9/21/11, Fabio Natali <[hidden email]> wrote:

> On 09/21/2011 07:57 AM, Andres Reyes wrote:
>> I believe that the best practice is to always use the {% url %} template
>> tag and not hardcoding your URL's
>
> That makes perfect sense. Thanks for your precious tip!
>
> So I added {% url add_to_cart %} to my template. However, it silenty
> fails and I am redirected to my main page with no error whatsoever.
>
> ### urls.py
> (r'^add_to_cart/$', add_to_cart)
>
> Any tips?
>
> Thanks and greetings, Fabio.
>
> --
> Fabio Natali FNstudio
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to
> [hidden email].
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

--
Sent from my mobile device

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: request.method not working as expected

Fabio Natali-2
On 09/21/2011 09:39 AM, Babatunde Akinyanmi wrote:
> What's in your add_to_cart view function?

Hi Babatunde,

at the moment, the add_to_cart view is just printing the POST data on
the screen.

Cheers, Fabio.

--
Fabio Natali FNstudio
http://fnstudio.it
fabio_natali@skype

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: request.method not working as expected

Fabio Natali-2
In reply to this post by Babatunde Akinyanmi
On 09/21/2011 09:37 AM, Babatunde Akinyanmi wrote:
> Your post showed the trailing url...

Yes! You are perfectly right, it was my fault! (But you have to admit it
was hard to find, that little trailing slash! :-))

> anyway I agree with your diagnosis.
[...]

Yes, I'll follow Andres Reyes advice and use {% url %} tag. I'm still
having a few problems with it, but it sounds like the way to go.

Thanks! Fabio.

--
Fabio Natali FNstudio

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: request.method not working as expected

Fabio Natali-2
On 09/21/2011 03:32 PM, Fabio Natali wrote:
[...]
> Yes, I'll follow Andres Reyes advice and use {% url %} tag. I'm still
> having a few problems with it, but it sounds like the way to go.

Ok, it works now. I just had to use the proper syntax! :-)

### urls.py
...
url(r'^add_to_cart/$', add_to_cart, name='add_to_cart'),

Cheers, Fabio.

--
Fabio Natali FNstudio

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.