title: LIKE button in a Django project
body: <p>There is a view in the Django project (a paginated blog) that is responsible for how likes work. It has one drawback: when a user likes a post, it is redirected to the main page of the site. How can I fix this so that the user would remain on the page where they liked.</p>
  <p>views.py</p>
  <pre><code>class AddLikeView(View):
       def post(self, request, *args, **kwargs):
           blog_post_id = int(request.POST.get('blog_post_id'))
           user_id = int(request.POST.get('user_id'))
           url_from = request.POST.get('url_from')
  
           user_inst = User.objects.get(id=user_id)
           blog_post_inst = News.objects.get(id=blog_post_id)
  
           try:
               blog_like_inst = BlogLikes.objects.get(blog_post=blog_post_inst, liked_by=user_inst)
           except Exception as e:
                blog_like = BlogLikes(blog_post=blog_post_inst,
                            liked_by=user_inst,
                            like=True)
                blog_like.save()
           return redirect(url_from)
  </code></pre>
  <p>template.py</p>
  <pre><code>&lt;form action=&quot;{% if not is_liked_bool %}{% url 'add' %}{% else %}{% url 'remove' %}{% endif %}&quot; method=&quot;post&quot;&gt;
  {% csrf_token %}
  &lt;input type=&quot;hidden&quot; name=&quot;blog_post_id&quot; value=&quot;{{ blog_post_id }}&quot;&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;user_id&quot; value=&quot;{% if user.is_authenticated %}{{ request.user.id }}{% else %}None{% endif %}&quot;&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;url_from&quot; value=&quot;{{ request.path }}&quot;&gt;
  
  {% if is_liked_bool %}
      &lt;input type=&quot;hidden&quot; name=&quot;blog_likes_id&quot; value=&quot;{{ blog_likes_id }}&quot;&gt;
  {% endif %}
  
  
  
  &lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;
      {% if not is_liked_bool %}
          &lt;i class=&quot;fi-xnluxl-heart&quot;&gt;♥&lt;/i&gt;
      {% else %}
          &lt;i class=&quot;fi-xnluxl-heart-solid&quot;&gt;♥&lt;/i&gt;
      {% endif %}
      &lt;span class=&quot;likes-qty&quot;&gt;{{ likes_counter }}&lt;/span&gt;
  &lt;/button&gt;
  </code></pre>