Django pagination not showing links to other page links - html

I am following a django book to learn django.I tried implementing pagination on a differnt website,it worked.But it's not working in my fyp.Each page has 3 objects as specified by me but the links to change pages are not appearing.
My views.py:-
from django.shortcuts import render
from .models import song_thumb
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
from django.views.generic import ListView
# Create your views here.
class SongListView(ListView):
model=song_thumb
context_object_name='artists'
paginate_by=3
template_name='home.html'
my template:
{% block content %}
<form class="" action="mediaplayer.html" method="get">
<div class="data-content">
{% for artist in artists %}
<div class="container">
<div id="img-1" class="tabcontent">
<div class="blog-content">
<div class="row">
<div class="col-sm">
<div class="card" style="width: 18rem;">
<div class="img">
<img class="img-thumbnail" src="{{ artist.img.url }}" alt="">
</div>
<div class="card-body">
<div class="title">
<p>{% trans 'Artist:' %} {{artist.artist}} </p><br>
<p>{% trans 'Title:' %} {{artist.song_title}}</p><br>
<p>{% trans 'Album:' %} {{artist.album}}</p><br>
<p>{% trans 'Duration' %} {{artist.song_duration}}</p><br>
</div>
<audio controls>
<source src='{{ artist.song.url }}' type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
</form>
{% include "pagination.html" with page=page_obj %}
{% endblock %}
my pagination.html:
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
Previous
{% endif %}
<span class="current">
Page {{ page.number }} of {{ page.paginator.num_pages }}.
</span>
{% if page.has_next %}
Next
{% endif %}
</span>
</div>
The pages have 3 objects each as specified by me.But the links to change page numbers are not showing up.

Change your pagination code like this:
<!-- Pagination -->
{% if is_paginated %}
<ul class="pagination justify-content-center mb-4">
{% if page_obj.has_previous %}
<li class="page-item">
<a class='page-link' href='?page=1'>First</a>
</li>
<li class="page-item">
<a class='page-link' href='?page={{ page_obj.previous_page_number }}'>«</a>
</li>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<li class="page-item disabled">
<a class='page-link' href='?page={{ num }}'>{{ num }}</a>
</li>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<li class="page-item">
<a class='page-link' href='?page={{ num }}'>{{ num }}</a>
</li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item">
<a class='page-link' href='?page={{ page_obj.next_page_number }}'>»</a>
</li>
<li class="page-item">
<a class='page-link' href='?page={{ page_obj.paginator.num_pages }}'>Last</a>
</li>
{% endif %}
</ul>
{% endif %}

No need to create separate page for pagination, you can use your template page also for same. I had same issue and for that i referred many solution and this one worked for me here is the pagination code I had used in my project
{% if is_paginated %}
{% if page_obj.has_previous %}
<a class="btn btn-outline-info mb-4" href="?page=1">First</a>
<a class="btn btn-outline-info mb-4" 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-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}">Next</a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
{% endif %}
{% endif %}
Here is the best tutorial about pagination i hope this will help you
https://www.youtube.com/watch?v=acOktTcTVEQ

Related

Active class on first element of nav-tab with pagination

I have a project where I have a paginated nav-tab with bootstrap.
The problem is that, anytime I refresh the page or I change the page with my pagination buttons, the active element is always the element n° 1 of the list.
This is probably due to the fact that I need to activate it upon opening the page with an if condition within the forloop. Let me post some code to be more clear
template
<input type="hidden" id="quizProfileId" value="{{pk}}">
<div class="card">
<div class="card-body">
<h4 class="card-title ms-3">New {{quiz_attempt.question.question_subject}} Quiz</h4>
<div class="row">
<div class="col-md-9">
<div class="tab-content text-muted mt-4 mt-md-0" id="v-pills-tabContent">
{% for quiz in quizzes %}
<div class="tab-pane {% if forloop.counter == 1 %}active{% endif %} ms-3 questionsObj" id="question{{quiz.question_draft.question.id}}" value="{{quiz.question_draft.question.id}}">
<h5 class="text-dark">{{quiz.question_draft.question.text}}</h5>
<div class="list-group mt-3">
{% for image in quiz.question_draft.question.questionimage_set.all %}
<a href="{{image.image.url}}" target="_blank">
<img src="{{image.image.url}}" alt="" width="100px" height="100px">
</a>
<br>
{% endfor %}
{% for answer in quiz.question_draft.answerdraft_set.all|shuffle %}
<a href="#"
data-pk="{{quiz.question_draft.question.id}}"
id="answer{{answer.answer.id}}" data-answerId="{{answer.answer.id}}"
class="
list-group-item answer question{{quiz.question_draft.question.id}} list-group-item-action
{% if quiz.question_draft.is_answered == True and answer.answer.is_correct == True %}bg-success{% endif %}
{% if answer.is_answered == True and answer.is_correct == True %}bg-success disabled
{% elif answer.is_answered == True and answer.is_correct == False %}bg-danger disabled
{% elif quiz.question_draft.is_answered == True %} disabled
{% endif %}
">
<b>{{forloop.counter0|to_char}}</b> &nbsp&nbsp {{answer.answer.text}}
</a>
{% endfor %}
</div>
</div>
{% endfor %}
</div>
</div>
<div class="col-md-3">
<div class="row">
<div class="form-group mt-3 mb-3">
{% if quiz_profile.is_completed != True %}
<form id="finishQuiz" action="{% url 'quiz:finish_test' pk=pk %}" method="get">
<button type="submit" class="btn btn-sm btn-success" id="finishButton" >Finish Test</button>
Save
Cancel Test
</form>
{% else %}
Quiz Report
{% endif %}
</div>
</div>
<div class="row">
<div class="nav nav-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
{% for quiz in pages %}
<a class="btn btn-sm btn-outline-secondary custom m-1
{% for answer in quiz.question_draft.answerdraft_set.all %}
{% if answer.is_answered == True and answer.is_correct == True %}bg-success{% endif %}
{% endfor %}
{% if quiz.question_draft.is_correct == False %}bg-danger{% endif %}
{% if forloop.counter == 1 %}active{% endif %} "
id="questionButton{{quiz.question_draft.question.id}}"
data-bs-toggle="pill" href="#question{{quiz.question_draft.question.id}}">{{ pages.start_index|add:forloop.counter0 }}
</a>
{% endfor %}
</div>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-md-9">
</div>
<div class="col-md-3">
<ul class="pagination">
{% if pages.has_previous %}
<li class="page-item">
<a class="page-link" href="{% url 'quiz:study_quiz' pk=pk %}?page={{ pages.previous_page_number }}"><i class="fas fa-angle-double-left"></i></a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#"><i class="fas fa-angle-double-left"></i></a>
</li>
{% endif %}
{% for i in paginator.page_range %}
<li class="page-item {% if pages.number == i%} active {% endif %} %}" >
<a class="page-link" href="{% url 'quiz:study_quiz' pk=pk %}?page={{ i }}" >{{i}}</a>
</li>
{% endfor %}
{% if pages.has_next %}
<li class="page-item">
<a class="page-link" href="{% url 'quiz:study_quiz' pk=pk %}?page={{ pages.next_page_number }}" ><i class="fas fa-angle-double-right"></i></a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#"><i class="fas fa-angle-double-right"></i></a>
</li>
{% endif %}
</ul>
</div>
</div>
</div>
</div>
I think that in particular {% if forloop.counter == 1 %}active{% endif %} is the main reason of this problem. On the other side, I don't know how to give the class active only to the first element when I load the page for the first time.
And in the same time, I would like to load the first element of the second page, when I load the second page and so on.

Building a dropdown filter

I was wondering if I can have assistance with building a dropdown filter for classes. I was looking at this example: https://codepen.io/luciopaiva/pen/YXYGYE?editors=101 but the difference is that I am using Twig from Advanced Custom Fields Plugin so I cannot manually input the data into the HTML. Here is the HTML I am using. I am trying to make it like this photo. See image here I don't have styling, but this is the logic I am trying to achieve. Basically I am trying to build a relative filter based on what the visitor clicks and have the correct box show below relative to the course. PLEASE HELP!! Thank you!
HTML:
<div class="course-selections filter-section without-background no-left-right">
<div class="customlayout">
{% set category = bloc.training_course_list_compact %}
<h2 class="center-title">{{ category.headline }}</h2>
<div class="inner-class-selection filter-inner-section">
<div class="filter-courses filter-area">
<div class="filter" id="course-solution">
<div class="upper-filter"><h3>Solutions</h3><i class="fas fa-angle-down"></i></div>
<div class="filter-dropdown">
{% for category in bloc.training_course_list_compact.course_categories %}
{% set titleupper = category.title %}
<h4>{{ titleupper|e('wp_kses_post') }}</h4>
{% endfor %}
</div>
</div>
<div class="filter" id="course-type">
<div class="upper-filter"><h3>Class Format</h3><i class="fas fa-angle-down"></i></div>
<div class="filter-dropdown">
{% for category in bloc.training_course_list_compact.course_categories %}
{% for course in category.get_field('courses') %}
{% for classformats in course.get_field('dates') %}
{% set classformat = classformats.class_format %}
{% if classformat == "Lecture" %}
<div class="class-types type-filter lecture"></div>
{% elseif classformat == "Online" %}
<div class="class-types type-filter online"></div>
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
<div class="class-title-type type-filter lecture-type"><h4></h4><span></span>
</div>
<div class="class-title-type type-filter online-type"><h4></h4><span></span>
</div>
</div>
</div>
<div class="filter" id="course-location">
<div class="upper-filter"><h3>Locations</h3><i class="fas fa-angle-down"></i></div>
<div class="filter-dropdown">
{% for category in bloc.training_course_list_compact.course_categories %}
{% for locationcourse in category.get_field('courses') %}
{% for location in locationcourse.get_field('dates') %}
<div class="course-location" data-value="{{ location.location }}"></div>
{% endfor %}
{% endfor %}
{% endfor %}
<div class="class-location type-filter"></div>
</div>
</div>
<div class="filter" id="course-date">
<div class="upper-filter"><h3>Start Date</h3><i class="fas fa-angle-down"></i></div>
<div class="filter-dropdown">
{% for category in bloc.training_course_list_compact.course_categories %}
{% for datecourse in category.get_field('courses') %}
{% for dates in datecourse.get_field('dates') %}
<div class="course-date" data-value="{{ dates.date }}"></div>
{% endfor %}
{% endfor %}
{% endfor %}
<div class="class-date type-filter"></div>
</div>
</div>
</div>
<div class="class-blocks">
{% for category in bloc.training_course_list_compact.course_categories %}
{% set thumbnail = category.thumbnail %}
{% set link = category.link %}
{% set titleupper = category.title %}
{% for course in category.get_field('courses') %}
{% set link = course.link %}
{% set title = course.title %}
<div class="class-block">
<a class="class-block-link" href="{{ link|e('esc_url') }}">
<div class="image">
</div>
<h3>{{ title|e('wp_kses_post') }}</h3>
<h4>{{ titleupper|e('wp_kses_post') }}</h4>
</a>
<a class="course-link" href="{{ link|e('esc_url') }}">Course</a>
</div>
{% endfor %}
{% endfor %}
</div>
</div>
</div>

how to find first image in a list of images in django

I have a Django template render result and images for search of cars. as follow:
{% for item in result %}
<li class="result-row">
<!-- image box -->
<span>
<a href="#" class="result-image-act" >
{% for image in item.images_cars_set.all %}
{% if image.car_images_exists %}
{% if image.car_images.0 %}
<img class="active" src=" {{image.car_images.url}}">
{% endif %}
{% if not image.car_images.0 %}
<img src=" {{image.car_images.url}}">
{% endif%}
{% endif %}
{% empty %}
<img src="{% static 'path/to/default image' %}" class="active">
{% endfor %}
</a>
<span class="embed-result-price">{{item.price}}</span>
<div class="swipe-wrap">
<div class="swipe-wrap-lef">
<span class="move" >
<div class="swipe-prev">
<p><</p>
</div>
</span>
</div>
<div class="swipe-wrap-rig">
<span class="move" >
<div class="swipe-next">
<p>></p>
</div>
</span>
</div>
</div>
</span>
The code is working fine except that I want to find the first image and put it in different img tag where it will take class="active". This class is used in javascript code to swipe all images to left or right. I tried to use {% if image.car_images.0 %} and {% if image.car_images.first %} to find first image but not success. what I get is all images without class of active. any help or suggestion.
You can use {% if forloop.counter0 == 0 %} do something {% endif %} or
{% if forloop.counter == 1 %} do something {% endif %} to do something for the 1st item when iterating with a {% for %} template tag.
More info: https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#for
Thus your code should be something like
<a href="#" class="result-image-act" >
{% for image in item.images_cars_set.all %}
{% if forloop.counter == 1 %}
<img class="active" src=" {{image.car_images.url}}">
{% else %}
<img src=" {{image.car_images.url}}">
{% endif %}
{% empty %}
<img src="{% static 'path/to/default image' %}" class="active">
{% endfor %}
</a>

How to display 24 items in a carousel - Jekyll

Im working in a project and currently I have a data file that display 24 clients in a page that im taking for a data file
{% for client in site.data.clients %}
<li>
<a href="{{client.URL}}" target="_blank">
<img src="{% asset_path {{client.image}} %}"
alt="{{ client.alt }}">
</a>
</li>
{% endfor %}
I would like to implement a carousel that display 12 items in one slide (and when clicked the arrow) display the other 12 items.
"Two slides with 12 elements each"
I've tried to implement that in the owl carousel but somehow is inserting me all the items in the first element of the first slide. still trying to find a way to split all the elements in two slides like the picture.
what i have implemented so far:
<div class="testi-service owl-carousel owl-theme ">
{% for client in site.data.clients %}
{% if forloop.index > 24 %}
{% assign slidenum = 2 %}
{% else %}
{% assign slidenum = 1 %}
{% endif %}
<div class="owl-item">
<div class="oc-item slide{{ slidenum }}">
<a href="{{client.URL}}" target="_blank">
<img src="{% asset_path {{client.image}} %}" alt="{{ client.alt }}">
</a>
</div>
</div>
{% endfor %}
</div>
</div>
This can be achieved with the following Liquid:
{% for client in site.data.clients %}
{% if forloop.index > 12 %}
{% assign slidenum = 2 %}
{% else %}
{% assign slidenum = 1 %}
{% endif %}
<li class="slide{{ slidenum }}">
<a href="{{client.URL}}" target="_blank">
<img src="{% asset_path {{client.image}} %}" alt="{{ client.alt }}">
</a>
</li>
{% endfor %}
And this css:
.slide2 {display:none;}
You can control the carousel with the following jQuery:
setInterval(function(){
$("[class^='slide']").toggle();
}, 3000);
Somehow it worked like this. just trying to fix the resizing and responsive problem at the moment
<div class=" col-md-9 clients side padding-content">
<div class="client-carousel">
<div class="container">
<div class="owl-item">
<div class="oc-item">
<ul class="clients-grid grid-6 nobottommargin clearfix">
{% comment %}
`site.data.clients` defined in `_data/clients.yml`
{% endcomment %}
{% for client in site.data.clients %}
{% if forloop.index > 24 %}
{% assign slidenum = 2 %}
{% else %}
{% assign slidenum = 1 %}
{% endif %}
<li class="slide{{ slidenum }}">
<a href="{{client.URL}}" target="_blank">
<img src="{% asset_path {{client.image}} %}" alt="{{ client.alt }}">
</a>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
<div class="container">
<div class="owl-item">
<div class="oc-item">
<ul class="clients-grid grid-6 nobottommargin clearfix">
{% comment %}
`site.data.clients` defined in `_data/clients.yml`
{% endcomment %}
{% for client in site.data.clients %}
{% if forloop.index > 24 %}
{% assign slidenum = 2 %}
{% else %}
{% assign slidenum = 1 %}
{% endif %}
<li class="slide{{ slidenum }}">
<a href="{{client.URL}}" target="_blank">
<img src="{% asset_path {{client.image}} %}" alt="{{ client.alt }}">
</a>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
</div>

how to set the whole back ground of col-sm-1 to #F7F7F7

my website uses bootstrap grid system of col-sm-1/col-sm-9/col-sm-2. And I want to the whole background of col-sm-1 to be #F7F7F7. Thing is I want it to be like the far left side of reddit
but when I tried to manipulate my web, I see only the div that has content on col-sm-1 changes its color.
<div class="col-sm-1">
<div class="row-fluid">
<h5 class="tag-name"></h>
</div>
{% if user.is_authenticated %}
{% if following %}
<h5 class="tag-named"><em>follow</em></h5>
<div class="tagging">
<div class="tab-pane{% if active_tab == 'connections' %} active{% endif %}" id="connections">
{% for a in following %}
<li style="padding:0.01em; list-style-type: square;"><a id="following-link" href="{{ a.get_absolute_url }}">{{ a }}</a></li>
<br />
{% endfor %}
</div>
</div>
{% else %}
<p class="tag-named" style="font-size:12px; text-align:left;"><em>enter followingcommunity <i class="fa fa-folder-open"></i>
hello</em></p>
{% endif %}
{% else %}
<p class="tag-named" style="font-size:12px; text-align:left;"><em>now login</em>
</p>
{% endif %}
</div>
please ignore django template language {{}}, as this needs to be done with awesome css.