Dan
Dan

Reputation: 379

Django : How do I send HTTP request through POST not using form

I have the following code in one of my template pages

<h4 id="upvote">
  <a href="/post/{{post.id}}/vote/1">Up</a>
</h4>
<h4 id="downvote">
  <a href="/post/{{post.id}}/vote/0">Down</a>
</h4>

At the moment, I'm not using a form but I wish to send these by HTTP POST

Do I need to use a form to do that or can it be done without one?

Upvotes: 0

Views: 82

Answers (1)

amethystdragon
amethystdragon

Reputation: 235

Typically I would use jQuery and its post method. This makes an ajax call to the server using POST without having to reload the page. (jQuery takes a little bit to learn but is soooo much easier to then JavaScript because of its shortcuts)

http://api.jquery.com/jquery.post/

$.ajax({
    type: "POST",
    url: "/post/{{post.id}}/vote/1",
    success: function(){
        //Do something here that lets the user know things worked out
    }
});

If you are not inclined to using jQuery you can make the same type of call using normal JavaScript, though it has a little bit more to it in my oppinion.

http://www.javascriptkit.com/dhtmltutors/ajaxgetpost2.shtml

Either way this could be applied to a button's "onclick" event and then the response could be handled and processed before being returned to the user. i.e. let the user know if the request was successful or failed.

EDIT: Not sure why you need a post for that it looks like because all of the data is contained in the URL and nothing is being sent along as part of the header, you could simply use a GET request.

Upvotes: 1

Related Questions