Reputation: 9289
After searching, I have yet to find the solution to my jquery-related question online.
I have multiple (div class='rate')
elements on a page that I would like to be updatable via ajax. index.html:
{% for thing in thing_list %}
<div class='rate'><a href='{% url thing_rating %}'>Rate</a></div>
{% endfor %}
They reference a url kind of like: urls.py
url(r'^(?P<object_id>\d+)/rate/(?P<score>[\d\-]+)/$', 'myview', name='thing_rating'),
I do check for request.is_ajax()
in the view: view.py:
def myview(request):
if request.is_ajax():
// doing some other stuff here
return render_to_response('rate.html', context_instance=RequestContext(request))
My ajax needs help..:
$( document ).ready( function() {
$('div#rate').click(function(){
$.ajax({
type: "POST",
url:"/????/",
data: { ????? },
success: function(data){ ????? },
error: function(){ alert("Error"); },
});
}
How do I reference the url in the above jquery? Via the same regex as in my urls.py?
What should I be passing as the data and success functions? All of the heavy lifting is done in my view..
Thanks for your ideas!
Upvotes: 1
Views: 527
Reputation: 7228
Since you are returning a rendered template. So, I would suggest using html dataType in your ajax() function and append it inside a div where you want to display it. Let me know if you have any other questions.
function vote(){
$('div#vote').click(function(e){
e.preventDefault();
var href = $(this).attr('href'); //Referring the anchor object which is clicked
$.ajax({
type: "POST",
url:href,
dataType: html,
success: function(data, status, xhr){ $('#div_to_load_html').html(data); },
error: function(){ alert("Error"); },
});
}
Upvotes: 1