Action without URL

29 views
Skip to first unread message

Karino Kang

unread,
May 8, 2014, 11:35:22 AM5/8/14
to django...@googlegroups.com
Dear all,

I'm a relatively new user of Django and I'm struggling to do something I'm not sure is possible with Django.

I wanted to develop a "like" app which act pretty much like the facebook "like" except that you can dislike.

My main problem is that I want to have a button or image for liking and disliking and I want to execute an action only in the database.

for example, for the like, I would have something in the template like :

<a href='#'><img src="like.png" /></a>

And I would have a python function defined somewhere in the app which would be the following :

def like(model):
    like_object = Like.objects.get(model=model, model_id=model_id)

    if like_object:
        like_object = Like(model=model, model_id=model_id, like_count=1)

    like_object.like_count = like_count + 1
    like_object.save()


I want this action to be executed when I click on the image like.png but I don't want the page to be change. There will be a like count displayed on the page but it will be updated using javascript.

Is this possible with Django?

Regards,
Karino KANG

Daniel Roseman

unread,
May 8, 2014, 11:54:48 AM5/8/14
to django...@googlegroups.com
Why would it not be possible? You are already aware that this needs to use Javascript, ie Ajax. Django won't do anything to stop you from making an Ajax request to update the counter.
--
DR. 

Stodge

unread,
May 8, 2014, 12:08:30 PM5/8/14
to django...@googlegroups.com
Yes it is possible - think of the actions that need to take place.

 * The user clicks the button in the browser.
 * The browser sends an Ajax request to Django
 * The Django view processes the Ajax request, updates the database and sends a response to the browser

These are web fundamentals and I think you need to do some reading on the basic server/client architecture.

Karino Kang

unread,
May 8, 2014, 12:15:27 PM5/8/14
to django...@googlegroups.com
Thank you all, I will check documentation for using Django with AJAX.

Regards,
Karino KANG
Reply all
Reply to author
Forward
0 new messages