[Answer]-Django: Jquery click function not working in Ajax

1👍

your code is good, the only thing what you have to do is to define your template in /tango/templates/rango/page_list.html. This template have the following code:

 {% if pages %}
<ul>
{% for page in pages %}
   <li>
   <a href="{% url 'goto' %}?page_id={{page.id}}"> {{ page.title}}</a>
   {% if page.views > 1 %}
      ({{page.views}} views)
   {% elif page.views == 1 %}
      ({{page.views}} view)
   {% endif %}
   </li>
{% endfor %}
</ul>
{% else %}
   <strong> No Pages currently in category. </strong>
{% endif %}

And inside of your category template you must define the following code:

% if category %}
    {% if user.is_authenticated %}
        <a href="/rango/category/{{ category_name_url }}/add_page/"> Add a new Page</a> <br>
    {% endif %}
    {% if pages %}
    <div id="pages">
        <ul>
        {% for page in pages %}
            <li>
                <a href="{% url 'goto' %}?page_id={{page.id}}"> {{ page.title}}</a>
            {% if page.views > 1 %}
                ({{page.views}} views)
            {% elif page.views == 1 %}
                ({{page.views}} view)
            {% endif %}
            </li>
        {% endfor %}
        </ul>
     </div>
    {% else %}
        <strong> No Pages currently in category. </strong>
    {% endif %}
{% else %}
    The specified category {{ category_name }} does not exist!
{% endif %}

0👍

I’m working through this section of the tutorial now and just want to add to Héctor’s answer. To avoid duplicating the code to display the list of pages I did the following:

I added a get_page_list() method to tango/rango/templatetags/rango_extras.py, similar to the get_category_list() method used to display a list of categories in an earlier section of the tutorial.

from rango.models import Page

@register.inclusion_tag("rango/page_list.html")
def get_page_list(category):
    pages = Page.objects.filter(category=category) if category else []
    return {'pages': pages}

Then we just need to load rango_extras and call the get_page_list() method in tango/templates/rango/category.html.

{% extends 'rango/base.html' %}

{% load rango_extras %}

<!-- Existing code -->

{% if category %}

    <!-- Existing code to show category likes and like button -->

    <div id="page_list">
        {% get_page_list category %}
    </div>

    <!-- Existing code to show search if user is authenticated -->

{% else %]
    The specified category {{ category_name }} does not exist!
{% endif %}

This allows you to display the list of pages when a category page is first loaded and then refresh it if a category is added from the search area, without having to duplicate any code.

Leave a comment