Well, I have fount the solution, but finally something is odd:

This is my ajax function:
<script type="text/javascript">
        $(document).ready(function() {
                $('#vote_form').submit(function(e) {
                        e.preventDefault();
                        var project_id = $('#project').val();
                        var user_id = $('#user').val();
                        var data = { project: project_id, user: user_id };
                        $.post(
                                {% url vote %},
                                data,
                                function (responseData) {
                                        $('#vote_button').disabled = true;
                                        $('#vote_div').innerHTML = 
responseData.response;
                                },
                                "json"
                        );
                });
        });
</script>


This is a html code:
<div id="vote_div" class="vote">
        <form id="vote_form" method="post">
                <input type="submit" id="vote_button" value="{% trans "Vote" %}"
class="submit">
                <input type="hidden" id="project" value="{{ project.id }}" />
                <input type="hidden" id="user" value="{{ user.id }}" />
                {% csrf_token %}
        </form>
</div>


This is my views.py method to handle the ajax:
#...@login_required

def vote_handler(request):
    if (request.method == 'POST'):

        project_id = request.POST.get('project', 0)
        user_id = request.POST.get('user', 0)
    else:
        return HttpResponse("{'response': 'error'}",
mimetype="application/json")


    try:

        project_id = int(project_id)
        project = Project.objects.get(id = project_id)
        user_id = int(user_id)

        user = User.objects.get(id = user_id)
    except ValueError:

        return HttpResponse("{'response': 'error'}",
mimetype="application/json")



    if request.is_ajax() and project and user:
        vote = Vote(project = project, user = user)
        vote.save()
        return HttpResponse("{'response': '1'}", mimetype="application/
json")

    else:

        return HttpResponse("{'response': 'error'}",
mimetype="application/json")


EVERYTHINS IS OK. THE VOTE IS BEING SAVED, BUT.... THE ONSUCCESS
FUNCTION OF AJAX CALL DOES NOT MAKE ANYTHING! So I can't hide the Vote
button or even alert the popup window for simple test.

Please, help. The code is clear except that function.

Kostia

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

Reply via email to