Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element disappears when I add an {% include %} tag inside my for loop

Tags:

python

django

Here's my template code for my comment queryset:

{% block comments %}
{% load el_pagination_tags %}
    {% paginate 10 comment_list %}
        {% for i in comment_list %}
            <div class='comment_div'>
                <div class="left_comment_div">
                    <p>{{ i.comment_text }}</p>
                </div>
            </div>
            {% include 'comment/comments.html' with comment_list=i.replies.all %}
        </div>
        {% endfor %}
    {% show_more %}  
{% endblock %}

I'm using django el pagination to implement ajax pagination. On the 3rd line you can see I initially load 10 comments. With this package, comes a {% show_more %} element I can add which, when clicked, will load the rest of the comments.

However, this {% show_more %}element disapears for some reason when I add {% include 'comment/comments.html' with comment_list=i.replies.all %}. For context, this include tag shows the replies for the current comment in the for loop.

Does anyone have any idea why this include tag affects the {% show_more %} element?

EDIT: Below is the relevent code for displaying show_more Github Source

el_pagination_tags.py

# show the template only if there is a next page
if page.has_next():
    print('Has next') #doesn't print
    request = context['request']
    page_number = page.next_page_number()
    # Generate the querystring.
    querystring_key = data['querystring_key']
    querystring = utils.get_querystring_for_page(
        request, page_number, querystring_key,
        default_number=data['default_number'])
    return {
        'label': label,
        'loading': loading,
        'class_name': class_name,
        'path': iri_to_uri(data['override_path'] or request.path),
        'querystring': querystring,
        'querystring_key': querystring_key,
        'request': request,
    }
# No next page, nothing to see.
print('No next') #prints for every comment (e.g. 20 times when 20 comments)
return {}

Can I change something in the above code to possibly make it work, or atleast debug further to help me find more about the problem? Help appreciated.

like image 330
Zorgan Avatar asked Oct 20 '17 10:10

Zorgan


3 Answers

You are probably consuming/changing variables in the included template that influence the behavior of show_more.

To debug you could either run via debugger and step into the show_more templatetag code or add print statements into that code (it's python so that works just fine).

The relevant code is in django-el-pagination/el_pagination/templatetags/el_pagination_tags.py#330 (https://github.com/shtalinberg/django-el-pagination):

# This template tag could raise a PaginationError: you have to call
# *paginate* or *lazy_paginate* before including the showmore template.
data = utils.get_data_from_context(context)
page = data['page']
# show the template only if there is a next page
if page.has_next():
    request = context['request']
    page_number = page.next_page_number()
    # Generate the querystring.
    querystring_key = data['querystring_key']
    querystring = utils.get_querystring_for_page(
        request, page_number, querystring_key,
        default_number=data['default_number'])
    return {
        'label': label,
        'loading': loading,
        'class_name': class_name,
        'path': iri_to_uri(data['override_path'] or request.path),
        'querystring': querystring,
        'querystring_key': querystring_key,
        'request': request,
    }
# No next page, nothing to see.
return {}

show_more will be empty if either:

  • page has reached the last iteration
  • querystring is empty (see the template django-el-pagination/el_pagination/templates/el_pagination/show_more.html)
like image 186
Risadinha Avatar answered Oct 23 '22 23:10

Risadinha


Solved by adding an if statement for my include tag like this:

        {% if i.replies.all %}
            {% include 'comment/comments.html' with comment_list=i.replies.all %}
        {% else %}

which only adds the include tag if that comment has 1 or more replies. Previously it would add an empty queryset if there were no replies, causing errors in the code.

like image 44
Zorgan Avatar answered Oct 24 '22 01:10

Zorgan


try changing the name here. comment_list to something else.

{% include 'comment/comments.html' with replies=i.replies.all %}
like image 45
Sandeep Balagopal Avatar answered Oct 24 '22 01:10

Sandeep Balagopal