user2270029
user2270029

Reputation: 871

jQuery to Make Ajax Request with Django

I am trying to interact with the server using jQuery and Django, but I am receiving this in my Chrome console:

POST http://127.0.0.1:8000/rate/ 500 (INTERNAL SERVER ERROR) jquery.min.js:6
x.support.cors.e.crossDomain.send jquery.min.js:6
x.extend.ajax jquery.min.js:6
x.(anonymous function) jquery.min.js:6
(anonymous function) jquery.js:14
x.event.dispatch jquery.min.js:5
y.handle

And this from my terminal:

rating = request.POST.get['rating']
TypeError: 'instancemethod' object is not subscriptable

Template:

{% extends "base.html" %}
{% block content %}

    <form action="#" id="rate-form">
        {% csrf_token %}
        <input type="radio" name="rating" value="like">Like<br>
        <input type="radio" name="rating" value="dislike">Dislike
        <input type="submit" value="Rate">
    </form>

{% endblock %}

jQuery:

$(document).ready(function(){
    $('#rate-form').submit(function(e){
        $.post( '/rate/', $(this).serialize(), function() {
            alert('Submitted'); 
        }); 
        e.preventDefault();
    });
});

Views:

def test(request):
    rating = request.POST.get['rating']
    html = "Your rating is %s" % rating
    return HttpResponse(html)

And my "/rate/" url is pointed to the test view.

Upvotes: 0

Views: 1660

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599580

This has nothing to do with Ajax or jQuery, it is a Python error. get is a method: you either do request.POST['rating'] or request.POST.get('rating').

Upvotes: 1

Related Questions