0

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.

1 Answer 1

1

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').

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I'm still not getting my data from the form in my view with that change, but I'm also not getting any errors now. Any ideas?
Not really. What do you see as the value of rating in the view?
So I did some testing and the page I have the form on is being loaded into part of the page a user is viewing without updating the whole page. When I put the form on the main template then it works fine, but then I don't have access to things like the post's id being hidden in the form. Thoughts?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.