Hi Ahmed, I was looking for that during several months and I have found examples than not run, and therefore I`ve get one conclution: "It`s not posible to get that functionality in Django". No one could demostrate with example code.
If you find something, please send me the code.
Thanks
Gabriel Araya Garcia
GMI - Desarrollo de Sistemas Informáticos
El dom., 17 may. 2020 a las 20:15, Ahmed Khairy (<ahmed.heshameldin@gmail.com>) escribió:
Hi all,--I need some help writing the ajax for a like button instead of refreshing every time a like is postedhere is the template:<form action="{% url 'score:like_post' post.pk %}" method='POST'>{% csrf_token %}{% if user.is_authenticated %}{% if liked %}<button type='submit' name='post_id' class= "btn btn-danger btn-sm" value="{{post.id}}"> Unlike </button>{% else %}<button type='submit' name='post_id' class= "btn btn-primary btn-sm" value="{{post.id}}"> Like </button>{% endif %}{% else %}<small><a href="{% url 'login' %}"> Login</a> to Like </small><strong>{{total_likes}} Likes </strong>{% endif %}</form>Here is the urls:path('like/<int:pk>', LikeView, name='like_post'),here is the views:def LikeView(request, pk):post = get_object_or_404(Post, id=request.POST.get('post_id'))like = Falsepost.likes.remove(request.user)like = Falseelse:post.likes.add(request.user)like = Truereturn redirect('score:post-detail', pk=pk)class PostDetailView(DetailView):model = Posttemplate_name = "post_detail.html"def get_context_data(self, *args, **kwargs):context = super(PostDetailView, self).get_context_data()stuff = get_object_or_404(Post, id=self.kwargs['pk'])total_likes = stuff.total_likes()liked = Falseliked = Truecontext["total_likes"] = total_likescontext["liked"] = likedreturn context
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/00707977-9b68-4329-a13d-adb9ab7b8813%40googlegroups.com.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKVvSDCuegzvsZQ_Rfms3yMA_EO914T0yJ_n929mnx7bNDodUA%40mail.gmail.com.
No comments:
Post a Comment