0

I'm a beginner in Django. Thanks for your help and patience.

I have a model which I use to show a list of posts. I am using django pagination as there are many posts. I want the visitor to be able to select some posts through checkboxes. The selected posts are then used for comparison on a seperate page.

In the html template, I have all the posts inside a form - each post is associated with a checkbox (checkbox is not part of the model). When a visitor selects some posts and clicks a submit button, then a view function returns a page with the selected posts for comparison. It all works fine, but the problem is with the pagination - i.e., when the visitor selects posts from different pages. For example, when selecting posts from the second page, those that were selected in the first page are not considered (no longer checked ?).

I have looked at using sessions, form wizard, etc. But I still can't figure out how they can help or what is the appropriate approach for me to investigate more.

Any guidance would be appreciated.

Below is a simplified version of my code:

models.py

class Cars(models.Model):
    title = models.CharField(max_length=85)
    prop1= IntegerField()
    prop2= IntegerField()
    prop3= IntegerField()

views.py (for the page with list of cars)

class CarListView(ListView):
    model = Cars
    template_name = 'Cars/Cars_list.html' # <app>/<model>_<viewtype>.html
    context_object_name = 'cars'
    paginate_by = 10

    def get_queryset(self):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Cars.objects.filter(author=user).order_by('-date_posted')

views.py (for the page where comparison of cars is made)

def CompareCars(request):
    Cars = Cars.objects.all()
    tableSize = Cars.objects.count()
    carList= []

    for Counter in range(0,tableSize):
        pk_value = Cars[Counter].pk
        checkboxName = 'checkbox_compare_car' + str(pk_value)
        if request.GET.get(checkboxName) == 'on':
            carList.append(pk_value)

    context = {
        'querySet': Cars,
        'carList': carList,
    }

    return render(request, 'Cars/Cars_compare.html', context)

Cars_list.html

    {% block content %}
        <form  action="{% url 'Cars_compare'%} " method="GET">
            {% for cars in page_obj %}
                <article>
                    <div class="media-body ">
                        <h6>>{{ cars.title }}</h6>
                        <h6> {{ cars.prop1 }}</h6>
                        <h6> {{ cars.prop2 }}</h6>
                        <h6> {{ cars.prop3 }}</h6>
                        <input class="form-check-input" type="checkbox"  name='checkbox_compare_car{{ cars.id }}'  id='checkbox_compare_car{{ cars.id }}'>
                        <button type='submit'> Compare </button>
                    </div>
                </article>
            {% endfor %}
        </form>

        <div class="pagination">
            <span class="step-links">
                {% if page_obj.has_previous %}
                <a class = "btn btn-sm" href="?page=1"> First</a>
                <a class = "btn btn-sm" href="?page={{ page_obj.previous_page_number }}">Previous</a>
                {% endif %}

                {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                <a class="btn btn-sm" href="?page={{ num }}">{{ num }}</a>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                <a class="btn btn-sm" href="?page={{ num }}">{{ num }}</a>
                {% endif %}
                {% endfor %}

                {% if page_obj.has_next %}
                <a class = "btn btn-sm" href="?page={{ page_obj.next_page_number }}"> Next </a>
                <a class = "btn btn-sm" href="?page={{ page_obj.paginator.num_pages }}">Last </a>
                {% endif %}
            </span>
        </div>  
    {% endblock content %}
Mahhas
  • 1
  • 2

0 Answers0