Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8565417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:25:20+00:00 2026-06-11T17:25:20+00:00

EDIT mayor changes were made to the question and text to make it clear.

  • 0

EDIT mayor changes were made to the question and text to make it clear. As it turns out the error is caused by a very stupid mistake which is that I forgot to import HttpResponse to my views.py. Since in the same views.py I handle another view, I assumed it was imported. Rookie mistake. 🙁

Question:
I’m attempting to submit up or down votes via ajax. Apparently most of the code in my view is executed, then when returning a response it fails. The problem is that there are no errors displayed by django; the only info I get in the terminal is this: “POST /c/vote/ HTTP/1.1” 500 10814. Where /c/vote/ is the URL that should handle votes.

The console in Chrome doesn’t help much either. The error I get is “Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR)”, then if I click the link I’m redirected to /c/vote/ where django returns a 404. Note I’m voting in a different url (i.e. /c/<country>/)

I found out that votes are actually being saved, or deleted so the problem may be when returning a response.

My template (custom template tags to know if the user voted before):

<div class="vote_buttons" x:id="{{linkpost.pk}}">
    <a href="#" class="upVote{% if linkpost|is_up_voted_by:user %} voted{%endif%}" x:value="1" ></a>
    <a href="#" class="downVote{% if linkpost|is_down_voted_by:user %} voted{%endif%}" x:value="-1"></a>

The js code (I’m using JQuery). The variable {{ vote_url }} is passed with a template tag.:

<script type="text/javascript">
    $(document).ready(function() {
        $(".vote_buttons").bind("vote", function(event, value) {
            var vote_el = $(this);
            $.ajax({
                    type:'POST',
                url: '{{ vote_url }}',
                data: {

                    'pk': vote_el.attr("x:id"),
                    'delta': value
                },
                dataType: "json",
        success : function(data, textStatus, jqXHR) {
                    switch (data.voted_as) {
                        case 1:
                            vote_el.find("a.upVote").addClass("voted");
                            vote_el.find("a.downVote").removeClass("voted");
                       break;
                        case -1:
                            vote_el.find("a.upVote").removeClass("voted");
                            vote_el.find("a.downVote").addClass("voted");
                       break;
                    }
                },
            });
        });
        $('.upVote, .downVote').click(function(){
            $(this).parent().trigger("vote", $(this).attr("x:value"));
            return false;
        });
    });
</script>

And finally the view. I added some print statements to figure out where does it fails. Thanks to @Steven the code executes all the way to Step 12. Then the previously mentioned error occurs.

def vote(request):
    """
    Likes or dislikes a linkpost.
    """
    print "Setp 1 ok!"
    #User must be authenticated to vote.
    if request.is_ajax():
        print "Step 2 ok"
        if request.method == 'POST' and request.user.is_authenticated():
            print "Step 3 ok"
            delta = request.POST['delta']
            # In case an error occurrs with delta value
            try:
                delta = int(delta)
                print "Step 4 ok"
            except ValueError:
                print "Error was value error"
                return HttpResponse("{'success': 'false'}")

            # You can only vote upwards or downwards    
            if not delta in (1, -1):
                print "Error was in delta"
                return HttpResponse("{'success': 'false'}")
            print "Step 5 ok"
            #We check if the linkpost actually exists!
            LinkPost = get_model('company', 'LinkPost')
            try:
                linkpost = LinkPost.objects.get(pk=request.POST['pk'])
                print "Step 6 ok"
            except LinkPost.DoesNotExist:
                print "Link object does not exist"
                return HttpResponse("{'success': 'false'}")

            #We check if the user voted before.
            Vote = get_model('company', 'Vote')
            try:
                vote = Vote.objects.get(linkpost = linkpost, listener = request.user)
                print "Step 7 ok!" 
            except Vote.DoesNotExist:
                print "Vote doesn't exists!" 
                vote = None

            # If there is already a vote
            if vote:

                print "Step 8."
                if vote.delta == delta:
                    vote.delete()
                else:
                    print "Step 9."
                    vote.delta = delta
                    vote.save()

            #There wasn't a vote, we create one.
            else:
                print "Step 10."
                Vote.objects.create(linkpost = linkpost,
                                           listener = request.user,
                                           delta = request.POST['delta'])

            response_dict = {'success' : 'true', 'voted_as': delta}          
            print "Step 12."    
            return HttpResponse(simplejson.dumps(response_dict), mimetype="application/json")
        else:
            print "User not authenticated"
            raise Http404('What are you doing here?')
    else:
        print "Request isn't ajax"
        raise Http404('What are you doing here?')

Any help figuring out the problem will be highly appreciated! If the question is still unclear I’ll try to clarify or add as much info as possible!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:25:21+00:00Added an answer on June 11, 2026 at 5:25 pm

    Try:

    response_dict = "{'success' : 'true', 'voted_as': '%s'}" % (delta)
    

    You have an extraneous s after the % operator in your code. It should be % (delta)

    Also, you should find that you can see Django’s informative error page in your browser developer tools (check the Network section), assuming you have DEBUG=True set. The formatting may be off, but it should still help you get to the code errors more quickly.


    Edit: OP fixed typo detailed below:

    If this isn’t a typo in your question: except Vote.DoesNotExists:

    then you may have more luck with:

    except Vote.DoesNotExist:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
Edit (updated question) I have a simple C program: // it is not important
EDIT: iam using ajax to load text in my content that is why onload
EDIT : It turned out that this can only be done through an external
EDIT: Simple version of the question: I want to create server variables in the
MAJOR EDIT: Problem was solved after reading Will Dean's comment. The original question is
Edit: In the previous version I used a very old ffmpeg API. I now
When starting out a new project, there are lot of changes in models that
Major Edit: I misread the article! The comment was in regards to the finalize
EDIT: Now a Major Motion Blog Post at http://messymatters.com/sealedbids The idea of rot13 is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.