title: How to write a Django view for a POST request
body: <p>I have written a very small example: a junit button which sends a POST request with a pair of values:</p>
  
  <pre><code>&lt;!doctype html&gt;
  
  &lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8" /&gt;
    &lt;title&gt;jQuery UI Button - Default functionality&lt;/title&gt;
      &lt;script src="{{STATIC_URL}}js/jquery-1.9.1.js"&gt;&lt;/script&gt;
      &lt;script src="{{STATIC_URL}}js/jquery-ui-1.10.3.custom.js"&gt;&lt;/script&gt;
      &lt;link rel="stylesheet" href="{{STATIC_URL}}css/jquery-ui-1.10.3.custom.css"&gt;
  
    &lt;script&gt;
    $(function() {
      $( "button" )
        .button()
        .click(function( event ) {
          var postdata = {
              'value1': 7,
              'value2': 5
          };
          $.post('', postdata); // POST request to the same view I am now
          window.alert("Hello world!"); // To know it is working
        });
    });
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;button&gt;Submit&lt;/button&gt;
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>So, the view is rendered when a GET request is sent to localhost:8000/button/, and when the button is pushed a POST request is also sent to localhost:8000/button/.</p>
  
  <h1>urls.py</h1>
  
  <pre><code>from django.conf.urls import patterns, url
  
  urlpatterns = patterns('',
      url(r'^button/$', 'helloworld.views.buttonExample'),
      )
  </code></pre>
  
  <h1>views.py</h1>
  
  <pre><code>def buttonExample(request):
      print 'RECEIVED REQUEST: ' + request.method
      if request.method == 'POST':
          print 'Hello'
      else: #GET
          return render(request, 'buttonExample.html')
  </code></pre>
  
  <p>When the GET request is done, the view is displayed correctly and I can also read at Django console the lines:</p>
  
  <pre><code>RECEIVED REQUEST: GET &lt;---- This line is because of my print
  [28/May/2013 05:20:30] "GET /button/ HTTP/1.1" 200 140898
  [28/May/2013 05:20:30] "GET /static/js/jquery-1.9.1.js HTTP/1.1" 304 0
  [28/May/2013 05:20:30] "GET /static/js/jquery-ui-1.10.3.custom.js HTTP/1.1" 304 0
  [28/May/2013 05:20:30] "GET /static/css/jquery-ui-1.10.3.custom.css HTTP/1.1" 304 0
  ...
  </code></pre>
  
  <p>And when the button is pushed, I can see:</p>
  
  <pre><code>[28/May/2013 05:20:34] "POST /register/ HTTP/1.1" 403 142238
  </code></pre>
  
  <p>But "RECEIVED REQUEST: POST" is never printed. Neither is "Hello". It seems like the urls.py is not serving the view when a POST arrived, because in Firebug I can see that POST status is 403 FORBIDDEN.</p>
  
  <p>This is probably a silly newbie mistake, but I don't know what am I missing. I have read the <a href="http://www.djangobook.com/en/2.0/chapter08.html" rel="noreferrer">django book chapter about advanced URLConf and Views</a>, and it looks like it should work just by checking request.method value.</p>