Search code examples
pythondjangodjango-viewsdjango-templatesrender-to-string

render_to_string seems to remove value of an variable


I am creating a like unlike button in Django and I ran into a wall. I am using ajax grab id and post to render to string. When button gets clicked first time everything is fine but render_to_string does not return value attr. And I have no idea why?

def in views I have:

def like_post(request):

# post = get_object_or_404(Post, id=request.POST['post_id'])
id = request.POST.get('id')

post = get_object_or_404(Post, id=id)
# check if this user already  
is_liked = False
if post.likes.filter(id=request.user.id).exists():
    post.likes.remove(request.user)
    is_liked = False
else:
    post.likes.add(request.user)
    is_liked = True
context = {
    'is_liked': is_liked,
    'value': id,
}

if request.is_ajax():
    html = render_to_string('post/like_section.html', context, request=request)
    return JsonResponse({'form': html})

section where this is being rendered:

  <div id="like-section">
     {% include 'post/like_section.html' %}
  </div>

my jquery

 $(document).on('click', '#like', function(e) {
    e.preventDefault();
    var id = $(this).attr("value");
    var url = '{% url "like_post" %}';

    $.ajax({
        type: 'POST',
        url: url,
        data: {
            id: id,
            csrfmiddlewaretoken: '{{ csrf_token }}'
        },
        dataType: 'json',
        success: function(response) {

            $('#like-section').html(response['form'])

        },
        error: function(rs, e) {
            console.log(rs.responseText)
        }
    });

});

section that is being rendered

  <form action='{% url "like_post" %}' method="post">

    {% csrf_token %} {% if is_liked %}

     <button name="post_id" class="likes" id="like" type="submit" value="{{ Post.id }}" like-count="{{ Post.likes.count }}">click 1</button> {% else %}
     <button name="post_id" class="likes" id="like" type="submit" value="{{ Post.id }}" like-count="{{ Post.likes.count }}">click 2</button> {% endif %}

  </form>

{{Post.id}} is not being rendered via render_to_string result I get back is this:

<form action='/like/' method="post">

    <input type="hidden" name="csrfmiddlewaretoken" value="0isfVRqNKFrDpFjCBi8jdFUgYzaw13YsEdPZV0dwi3SyExUmfOKLZUgFxaDAWhQ1"> 
    <button name="post_id" class="likes" id="like" type="submit" value="" like-count="">click 2</button> 

</form>

Seems like the variable is not being picked up, if I hardcode in any number and not pulling from db as {{ Post.id }} all seems to be working fine. Any idea what I am missing?

Thanks in advance


Solution

  • You did not pass Post to the context, so indeed the template engine can not render it. You should pass this, for example with:

    def like_post(request):
        id = request.POST.get('id')
        post = get_object_or_404(Post, id=id)
        # check if this user already  
        is_liked = False
        if post.likes.filter(id=request.user.id).exists():
            post.likes.remove(request.user)
            is_liked = False
        else:
            post.likes.add(request.user)
            is_liked = True
        context = {
            'is_liked': is_liked,
            'value': id,
            'Post': post  # ←  add post to the context
         }
        if request.is_ajax():
            html = render_to_string('post/like_section.html', context, request=request)
        return JsonResponse({'form': html})