On Oct 20, 8:23 am, Sithembewena Lloyd Dube <zebr...@gmail.com> wrote:
> Hi all,
>
> I have a page where users can rate a video, and in models.py I have a video
> model with a 'rating' property. I wish to find out how I can AJAXify the
> rating button on the page, so that if a user clicks on it, the button can
> call a custom function and pass the video id (cannot call a view as that
> would return an entire response). Am thinking of a function roughly as
> follows:
>
> from myproject.myapp.models import Video
>
> def save_rating(video_id, rating):
>     video = Video.objects.filter(pk=video_id)
>     video.rating += rating
>     video.save()
>     return video.rating #* The rating label on the page would then be
> updated with the returned rating.*
>
> I would like to do this without using a view or doing a form POST or GET of
> any kind. Any pointers?

AJAX does in fact do a POST or GET. So you do need to make a view
function that your javascript can issue a POST to update the rating.
Your javascript will need to pass the video ID to the view somehow,
either as part of the URL or as a POST parameter. Your view then needs
to return a response to the javascript, and you can do this as either
plain old text, JSON, XML, or HTML, depending on what makes the most
sense for you. I'd recommend you use a javascript library like jQuery,
simply because these libraries hide some of the very tedious things
you have to do if you were to code the request by hand.

I'd suggest you take a look at some jQuery AJAX tutorials on the web.

-- 
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