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 6122579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:54:56+00:00 2026-05-23T15:54:56+00:00

I have a Django view, named vote. It is protected by a @login_required decorator,

  • 0

I have a Django view, named vote. It is protected by a @login_required decorator, and in normal use works completely fine.

I decided it would be a worthwhile idea to start looking into ajax and javascript to make the system more dynamic, and so I implemented something like the below for my first try:

$(function() {
        $(".vote").click(vote);
});

var vote = function() {
            pk = $(this).attr('pk');

            $.ajax({
                type: "POST",
                data: "pk=" + $(this).attr("pk"),
                url: "/link/" + $(this).attr("pk") + "/vote/",
                });
        };

Which successfully POSTS to the correct URL. When I look at the output with firebug, I find I’m getting 500 errors. I’ve included the snipped from https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax into my pages, which I had thought would solve the CSRF issue, however it appears not to have done so.

Wondering whether I’m missing something obvious!

Thanks!

  • 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-05-23T15:54:57+00:00Added an answer on May 23, 2026 at 3:54 pm

    Turns out the way that I solved this was to tidy up the above:

    var vote = function() {
            pk = $(this).attr('pk');
    
            data = {
                'pk': pk
            };
    
            $.ajax({
                type: "POST",
                data: data,
                url: "/link/" + pk + "/vote/",
                });
        };
    

    Thus tidied, I then checked out the view, and discovered that it was not pulling the correct value out of the DataDict passed to it by ajax, which was where the 500 error was coming from.

    I had previously encountered a 403 due to the CSRF issue, for those wondering how to solve that, I simply used the script mentioned in the question above, saved in a ‘csrf.js’ file in the /static/js/ directory of my app, and then included that as one of the scripts, which then solved that issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a django view that returns HTTP 301 on a curl request: grapefruit:~
I have a Django view called change_priority. I'm posting a request to this view
I have a simple Django view that just returns URL parameters, but if I
I have 2 forms in my Django view. How can I do a check
I have such stuff in my django view: message = 'sometext' rating = 7
I have a Django project. Given a url, how can I know which view
I have the view function in django that written like a dispatcher calling other
I have the following models: http://slexy.org/view/s20T8yOiKZ from mxutils.cms_services import generate_secid from django.db import models
I am newbie at Django. I have model with a custom method. In view
i have problem with django's named urls which is included with include keyword. In

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.