Need recommendations or little help how to list insert in columns using Django built-in Pagination and Bootstrap.
For example. I have list with pagination. Every page have for example 9 results. But it all is in one column. I want to using Bootstrap row/column (col-md-4) to insert this list in 3 columns/3 rows. Is that with django Pagination possible or not? How? Or what you recommend?
This is my template code, where i make 3 columns with bootstrap. But all columns have 9 results and all column results are the same!
{% if project %}
<div class="container">
<div class="row">
<div class="col-md-4">
{% for projects in project %}
<li>
<a href="{{ current_page.get_absolute_url }}{{ project.slug }}">
<img src="{{ projects.main_image.image|thumbnail_url:'projects_top_thumbnail' }}" alt="{{ projects.title }}" />
<div class="showcase-title">
{{ projects.title }}
</div>
</a>
</li>
{% endfor %}
</div>
<div class="col-md-4">
{% for projects in project %}
<li>
<a href="{{ current_page.get_absolute_url }}{{ project.slug }}">
<img src="{{ projects.main_image.image|thumbnail_url:'projects_top_thumbnail' }}" alt="{{ projects.title }}" />
<div class="showcase-title">
{{ projects.title }}
</div>
</a>
</li>
{% endfor %}
</div>
<div class="col-md-4">
{% for projects in project %}
<li>
<a href="{{ current_page.get_absolute_url }}{{ project.slug }}">
<img src="{{ projects.main_image.image|thumbnail_url:'projects_top_thumbnail' }}" alt="{{ projects.title }}" />
<div class="showcase-title">
{{ projects.title }}
</div>
</a>
</li>
{% endfor %}
</div>
<div class="pagination">
<span class="step-links">
{% if project.has_previous %}
previous
{% endif %}
<span class="current">
Page {{ project.number }} of {{ project.paginator.num_pages }}.
</span>
{% if project.has_next %}
next
{% endif %}
</span>
</div>
</div>
</div>
{% endif %}
Try this
<div class="container">
<div class="row">
{% for projects in project %}
<div class="col-md-4">
<li>
<a href="{{ current_page.get_absolute_url }}{{ project.slug }}">
<img src="{{ projects.main_image.image|thumbnail_url:'projects_top_thumbnail' }}" alt="{{ projects.title }}" />
<div class="showcase-title">
{{ projects.title }}
</div>
</a>
</li>
</div>
{% endfor %}
</div>
<div class="pagination">
<span class="step-links">
{% if project.has_previous %}
previous
{% endif %}
<span class="current">
Page {{ project.number }} of {{ project.paginator.num_pages }}.
</span>
{% if project.has_next %}
next
{% endif %}
</span>
</div>
</div>
If you are looking for a grid of 3X3 Then the above code solves that.
Related
I am trying to set the replay button on my web application in Django, I am using django-messages, and I have set all libraries to the latest version for Django and python. Everything was working fine until I tried to set the replay button.
inbox.html
{% load static %}
{% block content %}
{% if user.is_authenticated %}
{% load i18n %}
{% if message_list %}
<section class="sidebar">
<div class="sidebar--inner">
<div class="is-settings--parent">
<div class="sidebar-menu">
<ul>
<li class="inboxItem isActive"><a href="#0">Inbox (<span class="numCount"></span>)
</a></li>
<li class="sentItem">Sent</li>
<li class="spamItem">Spam</li>
<li class="trashItem">Trash</li>
</ul>
</div>
</div>
</div>
</section>
<section class="view">
<section class="emails is-component">
<div class="emails--inner">
<div>
<h1 class="email-type">Inbox</h1>
{% for message in message_list %}
<div class="inbox">
<div class="email-card">
<div class="is-email--section has-img">
<div class="sender-img" style="">
</div>
</div>
<div class="is-email--section has-content">
<div class="sender-inner--content">
<p class="sender-name">From: {{ message.sender.username }}</p>
<p class="email-sum">Subject: <a href="{{ message.get_absolute_url }}">
{{ message.subject }}</a></p>
<p class="email-sum">Time: {{ message.sent_at|date:_("DATETIME_FORMAT")
</p>
</div>
</div>
<div class="email-action">
<span>
<a href="{% url 'messages_delete' message.id %}">
<img src="https://gmailapp.surge.sh/assets/images/trashcan.png" alt=""
class="action-icon trash">
</a>
</span>
<-- This line: -->
{% ifequal message.recipient.pk user.pk %}
<span>
<a href="{% url 'messages_reply' message.id %}">
<img src="" alt="" class="action-icon">
</a>
</span>
{% endifequal %}
</div>
</div>
</div>
{% endfor %}
</div>
</div>
</section>
{% endif %}
{% endblock %}
This is the error, I am getting after running the application on page inbox.html:
raise self.error(
django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 73: 'ifequal',
expected 'empty' or 'endfor'. Did you forget to register or load this tag?
I'm using jinja2 template with Flask and MongoDB and I need to display users' cars in a template.
I was able to render cars in the profile template with car information and buttons to edit or delete a car. The problem is that if a user has multiple cars, only the first DELETE CAR button actually works (where I have an include modals to confirm) but nothing happens when click on the second or third delete button. Apparently I duplicated the id that needs to be unique. But I cannot achieve the results.
Here is my template: profile.html
{% extends 'layout/base.html' %}
{% block content %}
{% include 'components/navigation/navigation.html' %}
<div class="container">
<div class="row g-3 justify-content-center">
<div class="col-6 mt-5">
<div class="card">
<div class="card-body">
<h3 class="text-center profileHeader">
{{ username }}'s profile
</h3>
</div>
</div>
</div>
</div>
<div class="row row justify-content-center mt-4">
{% for car in cars %}
{% if session.user|lower == car.created_by|lower %}
<div class="card mb-3" style="max-width: 840px;">
<div class="row g-0">
<div class="col-md-4">
<img src="{{ car.car_image }}" class="imgCard" alt="ferrari image">
</div>
<div class="col-md-8">
<div class="card-body">
<ul class="list-group list-group-flush">
<li class="list-group-item"><strong>Year</strong>: {{ car.car_year }} </li>
<li class="list-group-item"><strong>Name</strong>: {{ car.car_name }}</li>
<li class="list-group-item"><strong>Designer</strong>: {{ car.car_design }}</li>
</ul>
<ul class="list-group list-group-flush">
<li class="list-group-item"><strong>Engine</strong>: {{ car.spec_engine }}</li>
<li class="list-group-item"><strong>Power</strong>: {{ car.car_power }}</li>
<li class="list-group-item"><strong>Trasmission</strong>: {{ car.trasmission }}</li>
<li class="list-group-item"><strong>Races</strong>: {{ car.races }}</li>
<li class="list-group-item"><strong>Wins</strong>: {{ car.wins }}</li>
<li class="list-group-item"><strong>Podiums</strong>: {{ car.podiums }}</li>
<li class="list-group-item"><strong>Poles</strong>: {{ car.poles }}</li>
<li class="list-group-item"><strong>Fast Laps</strong>: {{ car.fast_laps }}</li>
<li class="list-group-item"><strong>Constructor Championship</strong>: {{
car.constructor_champ
}}</li>
<li class="list-group-item"><strong>Driver Championship</strong>: {{ car.drivers_champ }}
</li>
<li class="list-group-item"><strong>Description</strong>: {{ car.description }}</li>
<p><em>by: {{ car.created_by }}</em></p>
</ul>
<div class="d-grid gap-2 d-md-flex justify-content-md-end">
<a id="myBtn" class="btn btn-danger"><i class=" fas fa-trash-alt"></i> Delete</a>
<a href="{{ url_for('editcar', car_id=car._id) }}" class="btn btn-danger" id="edit-btn"><i
class="far fa-edit"></i> Edit</a>
{% with car_id=car._id %}
{% include 'components/modals/confirm.html' %}
{% endwith %}
</div>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
enter code here
</div>
{% endblock %}
Just make unique the tag id of delete button, so the trigger will be unique in every button. How to make unique ? just add the car._id on tag id
<a id="myBtn-{{car._id}}" class="btn btn-danger"><i class=" fas fa-trash-alt"></i> Delete</a>
As #Darn pointed out, your ids need to be unique. But that is not the cause of your issue here.
I assume you have some Javascript code that gets triggered when user clicks on the delete link. The issue is probably from how that code is being triggered. The code needs to be written in such a way that it is bound to dynamically generated elements (you're generating the delete links on the fly i.e. afte the code for the delete was already bound; that delete code needs to also apply to each element when it is created).
You should bind the delete action to a class e.g.
$(".fa-trash-alt").on("click", function(){
$(this).parent() ...... // this is the link to the instance of the link that was clicked
});
So I'm trying to make a button which highlights as active when the page is the same as page number, but it does not work, It only highlights page 1, When I go to page 2 then page 1 is still highlighed, index function handles the page I'm talking about.
views.py
from django.shortcuts import render
from .models import Listing
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
# Create your views here.
def index(request):
listings = Listing.objects.all()
paginator = Paginator(listings, 3)
page = request.GET.get('page')
paged_listings = paginator.get_page(page)
params = {'listings':paged_listings}
return render(request, 'listings/listings.html', params)
def listing(request, listing_id):
return render(request, 'listings/listing.html')
def search(request):
return render(request, 'listings/search.html')
listings.html
{% extends 'base.html' %}
{% block content %}
{% load humanize %}
<!-- Breadcrumb -->
<section id="bc" class="mt-3">
<div class="container">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item">
<a href="{% url 'index' %}">
<i class="fas fa-home"></i> Home</a>
</li>
<li class="breadcrumb-item active"> Browse Listings</li>
</ol>
</nav>
</div>
</section>
<!-- Listings -->
<section id="listings" class="py-4">
<div class="container">
<div class="row">
{% if listings %}
{% for listing in listings %}
<div class="col-md-6 col-lg-4 mb-4">
<div class="card listing-preview">
<img class="card-img-top" src="{{ listing.photo_main.url }}" alt="">
<div class="card-img-overlay">
<h2>
<span class="badge badge-secondary text-white">${{ listing.price | intcomma}}</span>
</h2>
</div>
<div class="card-body">
<div class="listing-heading text-center">
<h4 class="text-primary">{{ listing.title }}</h4>
<p>
<i class="fas fa-map-marker text-secondary"></i>{{ listing.city }} {{ listing.state }}, {{ listing.zipcode }}</p>
</div>
<hr>
<div class="row py-2 text-secondary">
<div class="col-6">
<i class="fas fa-th-large"></i>Sqfit: {{ listing.sqft }}</div>
<div class="col-6">
<i class="fas fa-car"></i>Garage: {{ listing.garage }}</div>
</div>
<div class="row py-2 text-secondary">
<div class="col-6">
<i class="fas fa-bed"></i>Bedrooms: {{ listing.bedrooms }}</div>
<div class="col-6">
<i class="fas fa-bath"></i>Bathrooms: {{ listing.bathrooms }}</div>
</div>
<hr>
<div class="row py-2 text-secondary">
<div class="col-12">
<i class="fas fa-user"></i>{{ listing.realtor.name }}</div>
</div>
<div class="row text-secondary pb-2">
<div class="col-6">
<i class="fas fa-clock"></i>{{ listing.list_date | timesince }}</div>
</div>
<hr>
More Info
</div>
</div>
</div>
{% endfor %}
{% else %}
<div class="col-md-12">
<p>No Listings Available</p>
</div>
{% endif %}
<!-- Footer -->
<div class="row">
<div class="col-md-12">
{% if listings.has_other_pages %}
<ul class="pagination">
{% if listings.has_previous %}
<li class="page-item">
«
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">«</a>
</li>
{% endif %}
</ul>
{% endif %}
{% for i in listings.paginator.page_range %}
{% if listings.number == i %}
<li class="page-item active">
<a class="page-link page-item">{{ i }}</a>
</li>
{% else %}
<li class="page-item">
{{i}}
</li>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</section>
{% endblock %}
You mistyped argument in page links. Just replace
<li class="page-item">
{{i}}
</li>
with
<li class="page-item">
{{i}}
</li>
(difference is equal sign = between query parameter and value)
The problem seems to be your use of
{% for i in listings.paginator.page_range %}
As this is returning a range, I think you should be using:
{% for i in listings.paginator %}
I'm new to Django and I'm trying to output first picture of the model in the first div and all the other in the second div.
I googled and didn't find any solutions. I'm sure that my solution is awful a bit.
<div class="carousel-item active">
<img src="{{ carousels[0].image.url }}" class="d-block w-100" alt="...">
</div>
{% for carousel in carousels([1]) %}
<div class="carousel-item">
<img src="{{ carousel.image.url }}" class="d-block w-100" alt="...">
</div>
{% endfor %}
View:
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
carousels = Carousel.objects.all()
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request,
'shop/product/list.html',
{'category': category,
'categories': categories,
'products': products,
'carousels': carousels})
If you're using Jinja, then you can just use regular slicing ([1:] for "everything starting from the second item"):
<div class="carousel-item active">
<img src="{{ carousels[0].image.url }}" class="d-block w-100" alt="...">
</div>
{% for carousel in carousels[1:] %}
<div class="carousel-item">
<img src="{{ carousel.image.url }}" class="d-block w-100" alt="...">
</div>
{% endfor %}
The analogue in vanilla Django templates is the slice filter:
{% for carousel in carousels|slice:"1:" %}
It should be fine for a first approach of jinja
{% for carousel in carousels %}
{% if forloop.counter == 0 %}
<div class="carousel-item active">
<img src="{{ carousel.image.url }}" class="d-block w-100" alt="...">
</div>
{% else %}
<div class="carousel-item">
<img src="{{ carousel.image.url }}" class="d-block w-100" alt="...">
</div>
{% endif %}
{% endfor %}
This code already have done the pagination ,but it false because per page it shows all items in the database ,and the same in other pages
This is the routing
#app.route('/shop/page/<int:page_num>')
def shop(page_num):
products=Product.query.filter
(Product.category.has(Product.category_id))
shopPage=Product.query.paginate
(page=page_num,per_page=5,error_out=True)
return render_template('shop.html',
shopPage=shopPage,products=products)
And this is the HTML code of pagination and the output products of database
{% block content %}
<div class="mb-5">
{% for product in products %}
<div class="col-4 mt-3 ">
<div class="card">
<div class="card-header">
<form method="POST" action="{{ url_for('shop') }}" >
</form>
</div>
<div class="card-body">
<h5 class="card-title">{{product.name }}</h5>
<p class="card-text">
<div>Category: {{ product.category.name }}</div>
<div>Price: {{ product.price }} $</div>
<div>Company: {{ product.company }}</div>
</p>
</div>
</div>
</div>
{% endfor %}
</div>
{% for page in shopPage.iter_pages(left_edge=2, right_edge=2, left_current=1, right_current=2) %}
{% if page %}
<nav >
<ul class="pagination ">
<li class="page-item ">
<a class="page-link" href="{{url_for('shop',page_num=page)}}" tabindex="-1">{{ page }}</a>
</li>
</ul>
</nav>
{% else %}
...
{% endif %}
{% endfor %}
{% endblock %}