Re: Updating a video rating using AJAX

2010-10-20 Thread Nicolas R
When you change data on your database ALWAYS use the http POST method.

So your view should check if request.method == 'POST' and then proceed
to change the rating. It should also check if the user is
authenticated and if she already voted.

As for the ajax part have a look at the jQuery javascript library. You
could do something like this:

$('#my_form').live('submit',function(){
var self = $this;
$.post( self.attr('action'), function(r){
self.replaceWith( $(r).find('#my_form') );
});
return false;
});

The javascript code you should write depends on how your app and views
work. The above snippet assumes that the url where the form submits
will return the form (even if there is an error).




On Oct 20, 4:23 pm, Sithembewena Lloyd Dube  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?
>
> Thanks.
>
> --
> Regards,
> Sithembewena Lloyd Dubehttp://www.lloyddube.com

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



Re: Updating a video rating using AJAX

2010-10-20 Thread brad
Also, if you DO choose to pass JSON back to your client, you should
serialize it first.  See this:

http://docs.djangoproject.com/en/dev/topics/serialization/

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



Re: Updating a video rating using AJAX

2010-10-20 Thread Brian Neal
On Oct 20, 8:23 am, Sithembewena Lloyd Dube  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.



Re: Updating a video rating using AJAX

2010-10-20 Thread brad

> I would like to do this without using a view or doing a form POST or GET of
> any kind. Any pointers?

Why?  If you're building a web app, you should use HTTP to for the
client to communicate with the app (AJAX is just another form of that
communication).

You're almost there, though, because your function is pretty close to
a view:

def save_rating(request, video_id, rating):
if request.is_ajax():
# the rest of your code...

json_content = '{"rating":"%s"}' % video.rating
return HttpResponse(json_content, mimetype='application/json')

Just be sure to return some sort of data that your client-side
javascript can interpret, such as JSON (of which I've given you an
untested example).

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