Roman Rdgz
Roman Rdgz

Reputation: 13254

POST request is not catched by Django server

I have a Django server running localy at http://localhost:8000, with a view set over url /register/.

There I have an html file with a form. The view is correctly displayed. But when I push a button, a POST request should be generated, but Django doesn't receive any request.

I don't know if I am setting a wrong host to make the post or I'm having a different trouble, because when testing at jsFiddle from Firefox for windows, I can see the POST with Firebug; but if I use Firefox&Firebug for Ubuntu, at the virtual machine where I'm running Django, no POST request is catched by Firebug.

#views.py
def formExample1(request):
    print 'RECEIVED REQUEST: ' + request.method
    if request.method == 'POST':
        startHour = request.REQUEST['starthour']
        endHour = request.REQUEST['endhour']
        return HttpResponseRedirect('http://www.google.es')
    else: #GET
        return render(request, 'formExample1.html')

urls.py

urlpatterns = patterns('',
    url(r'^register/$', 'helloworld.views.formExample1'),
)

And formExample1.html looks like this:

<script>
 ...
    // Play button
    $( "#id_playButton" ).click(function() {
        var postdata = {
            'starthour': $("#id_interval").slider("values", 0),
            'endhour': $("#id_interval").slider("values", 1)
        }
        $.post('register/', postdata)
    });
</script>

Upvotes: 1

Views: 370

Answers (2)

Glyn Jackson
Glyn Jackson

Reputation: 8354

I there are few clear issues with your example, you should read up on forms.

Here is a quick example using your code...

def formExample1(request):
    print 'RECEIVED REQUEST: ' + request.method
    if request.method == 'POST':
        form = YOURFORM(request.POST)
        if form.is_valid():
              startHour = form.cleaned_data['starthour']
              endHour = form.cleaned_data['endhour']

If you want to use the POST values (bad idea) then...

def formExample1(request):
    print 'RECEIVED REQUEST: ' + request.method
    if request.method == 'POST':
           startHour = request.POST['starthour'] 

    etc

However, even this still could be done better, why are you even assigned startHour etc here?

The last issue (if the post data is still not being sent) is a jquery issue. Check out this post: Get POST data in django form AJAX from

By the looks of it your are also not posting a real form and therefor missing csrf_token, you need to add this into your script i.e....

 csrfmiddlewaretoken: '{{ csrf_token }}'

Or if you want (but there are reasons against this) just add @csrf_exempt above your view make it exempt.

Upvotes: 1

Teshte
Teshte

Reputation: 632

This is how i make a post using jquery Things that bother me at your example are the fact that you post at "register/"...i don't use Django (webapp2 instead)..but shouldn't it be "/register" ?

var data = {"key":link.id};
$.ajax({
  type: "POST",
  url: "/deleteorders" , //+ link.id
  data: data,
  success: function(data){
    //here you do the processing needed to remove the row from the html;
    $('#'+link.id).parent().remove();
  }
}); 

Upvotes: 0

Related Questions