Hi I'm trying to develop an online store website using Django and I don't know why, but my price counter is not working. It was working all fine before I added some pagination, and now it's not adding all the values.Can anyone please help me out?
My views.py:
def cart(request):
cart = Cart.objects.all()[0]
context = {"cart":cart}
template = 'shopping_cart/cart.html'
return render(request, template, context)
def add_to_cart(request, slug):
cart = Cart.objects.all()[0]
try:
product = Product.objects.get(slug=slug)
except Product.DoesNotExist:
pass
except:
pass
if not product in cart.products.all():
cart.products.add(product)
messages.success(request, mark_safe("Product added to cart. Go to <a href='cart/'>cart</a>"))
return redirect('myshop-home')
else:
cart.products.remove(product)
messages.success(request, mark_safe("Product removed from cart"))
new_total = 0.00
for item in cart.products.all():
new_total += float(item.price)
cart.total = new_total
cart.save()
return HttpResponseRedirect(reverse('cart'))
My index.html(where I added pagination):
{% extends 'base.html' %}
{% block content %}
<h1>Products</h1>
<div class="container-md">
<div class="row">
{% for product in products %}
<div class="col">
<div class="card-deck" style="width: 18rem;">
<img src="{{ product.image_url }}" class="card-img-top" alt="...">
<div class="card-body">
<a class="card-title text-dark" href="{% url 'detail-view' product.slug %}">{{ product.name }}</a>
<p class="card-text">${{ product.price }}</p>
{% if not product.cart_set.exists %}
Add to Cart
{% else %}
Remove from Cart
{% endif %}
</div>
</div>
</div>
{% endfor %}
</div>
</div>
<div>
<ul class="pagination justify-content-center">
{% if is_paginated %}
{% if page_obj.has_previous %}
<a class="btn btn-outline-dark mb-4" href="?page=1">First</a>
<a class="btn btn-outline-dark 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-dark 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-dark mb-4" href="?page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="btn btn-outline-dark mb-4" href="?page={{ page_obj.next_page_number }}">Next</a>
<a class="btn btn-outline-dark mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
{% endif %}
{% endif %}
</ul>
</div>
{% endblock %}
My homeview:
class homeview(ListView):
model = Product
paginate_by = 6
context_object_name = 'products'
template_name = 'index.html'
My urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('cart/', sc_views.cart, name='cart'),
path('cart/<str:slug>/', sc_views.add_to_cart, name='add-to-cart'),
path('', include('products.urls'))
]
Related
I am having a problem in categorize Posts. I have categories in Post Model, Please help me solve this issue!
MODELS.PY
html= "html"
css= "css"
python= "python"
javascript = "javascript"
Lang=(
(html,"HTML"),
(css,"CSS"),
(python,"PYTHON"),
(javascript,"JAVASCRIPT")
)
Broilerplate = "Broilerplate"
Simple_Tags = "Simple Tags"
Elements = "Elements"
Webpages = "Webpages"
Category = (
(Broilerplate,"BROILERPLATE"),
(Simple_Tags,"Simple tags"),
(Elements,"Elements"),
(Webpages,"Webpages")
)
class Post(models.Model):
title = models.CharField(max_length=75)
subtitle = models.CharField(max_length=150)
language_1 = models.CharField(max_length=100, choices=Lang, default=html)
content_1= models.TextField(blank=True)
language_2 = models.CharField(max_length=100, choices=Lang,blank=True)
content_2= models.TextField(blank=True)
language_3 = models.CharField(max_length=100, choices=Lang,blank=True)
content_3= models.TextField(blank=True)
language_4 = models.CharField(max_length=100, choices=Lang,blank=True)
content_4= models.TextField(blank=True)
language_5 = models.CharField(max_length=100, choices=Lang,blank=True)
content_5= models.TextField(blank=True)
category= models.CharField(max_length=100, choices=Category, default=Broilerplate)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User,on_delete=models.CASCADE)
def __str__(self):
return self.title
# def update(self):
# updated_at = timezone.now()
# self.save(updated_at)
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk':self.pk})
VIEWS.PY
def search_by_category(request, category):
if request.post.objects.filter(pk=request.post.id).first().category == 'Broilerplate':
nor = request.post.objects.filter(pk=request.post.id).first().category
print(nor)
posts = Post.objects.filter(category='Broilerplate')
category = posts.first().category
print(category)
context = {'posts': posts, 'title': category , 'category': category}
return render(request, 'category.html', context)
else:
context = {'title': category , 'category': category}
return render(request, 'category.html', context)
URLS.PY
urlpatterns = [
path('html/',PostListView.as_view(), name='CodeWriter_Homepage'),
path('python/',views.CodeWriter_homepage_python, name='CodeWriter_Homepage_python'),
path('search/', views.search, name='search'),
path('categroize-by/<str:category>/', views.search_by_category, name='search_by_category'),
path('post-details/<int:pk>/',PostDetailView.as_view(), name='Post_details_page'),
path('post-update/<int:pk>/',PostUpdateView.as_view(), name='Post_update_page'),
path('post-delete/<int:pk>/',PostDeleteView.as_view(), name='Post_delete_page'),
path('user/<str:username>/', UserPostListView.as_view(), name='user-post'),
path('new-post/',PostCreateView.as_view(), name='New-post_page')
]
CATEGORY.html
How can I get the data of the post category in views.py?That will help me very much!
{% block main_post_body %}
<main class="post-body normal" >
<p class="post-heading"> Post for <b>{{ category }}</b> :</p>
{% comment %} <h1 class="mb-3">Search results for <b>{{category}}</b>: </h1> {% endcomment %}
{% comment %} {% if posts|length < 1 %}
<h3>No search results found</h3>
<p>Your search results for <b>{{ query }}</b> is not found.</p>
{% endif %} {% endcomment %}
{% for post in posts %}
<div class="row">
<div class="col span-8-of-12 card">
<div class="row">
<!-- <div class="triangle"></div>
<div class="person-info-card">
<div class="row">
<div class="col"><img src="{% static 'resoures/css/img/Me.jpg' %}" alt=""></div>
<div class="col">
<p>Ketan Vishwakarma</p>
<p class="post-no">126 Posts | 1k Followers</p>
</div>
</div>
</div> -->
<div class="col span-1-of-12">
<div class="person-img" id="person-info-card-toggle">
<img src="{{ post.author.profile.image.url }}" alt="" />
</div>
</div>
<div class="col span-7-of-12">
<h3>{{ post.title }} </h3>
<h4>{{ post.subtitle }}</h4>
</div>
<div class="col span-1-of-12">
<span class="ion-md-heart-empty"></span>
</div>
<div class="col span-1-of-12">
<span class="ion-md-add-circle-outline"></span>
</div>
<div class="col span-1-of-12 share">
<span class="ion-md-share"></span>
<ul class="dropdown-list-share">
<li><span class="ion-md-copy"> Copy</span></li>
<li><span class="ion-logo-whatsapp"> Share on Whatsapp</span></li>
<li><span class="ion-logo-facebook"> Share on facebook</span></li>
<li><span class="ion-md-more"> More</span></li>
</ul>
</div>
<div class="col span-1-of-12">
<span class="ion-md-menu"></span>
<ul class="dropdown-list">
<li>Doesn't work</li>
<li>Report</li>
<li>Report</li>
</ul>
</div>
</div>
<div class="tabs">
<ul class="tabs-list">
<li class="active">{{ post.language_1 }}</li>
{% if post.content_2 == "" %}
{% else %}
<li class="not-clickable hint--info" aria-label="To view this click Read more">{{ post.language_2 }}</li>
{% endif %}
{% if post.content_3 == "" %}
{% else %}
<li class="not-clickable hint--info" aria-label="To view this click Read more">{{ post.language_3 }}</li>
{% endif %}
{% if post.content_4 == "" %}
{% else %}
<li class="not-clickable hint--info" aria-label="To view this click Read more">{{ post.language_4 }}</li>
{% endif %}
{% if post.content_5 == "" %}
{% else %}
<li class="not-clickable hint--info" aria-label="To view this click Read more">{{ post.language_5 }}</li>
{% endif %}
<ul class="categories">
<abbr title="Categiores">
<a href="">
<span class="ion-md-code-working"></span>
{{ post.category }}
</a>
</abbr>
<!-- <li>Broilerplate</li> -->
</ul>
</ul>
<div id="tab1" class="tab active">
<p>
<pre><code class="language-{{ post.language_1 }}">
{{ post.content_1 }}
</code></pre>
</p>
</div>
<div id="tab2" class="tab">
<p>
<pre><code class="language-{{ post.language_2 }}">
{{ post.content_2 }}
</code></pre>
</p>
</div>
<div id="tab3" class="tab">
<p>
<pre><code class="language-{{ post.language_3 }}">
{{ post.content_3 }}
</code></pre>
</p>
</div>
<div id="tab4" class="tab">
<p>
<pre><code class="language-{{ post.language_4 }}">
{{ post.content_4 }}
</code></pre>
</p>
</div>
<div id="tab5" class="tab">
<p>
<pre><code class="language-{{ post.language_5 }}">
{{ post.content_5 }}
</code></pre>
</p>
</div>
</div>
<span class="ion-md-arrow-round-down"> Read More</span>
<h6>{{ post.date_posted|date:"F d, Y" }}</h6>
</div>
<div class="col span-4-of-12 main-card ">
<div class="card-small">
<h1>Result: </h1>
<div class="result-box">
<iframe src="" frameborder="0">just for fun</iframe>
</div>
<h6>{{ post.date_posted|date:"F d, Y" }}</</h6>
</div>
</div>
</div>
{% endfor %}
</main>
{% 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 %}
{% endblock main_post_body %}
NoteI have registered the Post model in admin.py
Tell me the solution to this problem I cannot get the solution I have been trying it for so long and am stuck on this.
So I'm trying to use pagination, When I visit the page I get MultiValueDictKeyError at /products/
'page', and when I go to localhost:8000/thatpage/?page=1 then I get TypeError at /products/
'Paginator' object is not callable.
This error occurs when i visit the page and i don't know what i'm doing wrong, Any help would be great.
views.py
def showproducts(request):
oof = CartItem.objects.filter(user=request.user).values_list('book', flat=True)
lmao = OrderItem.objects.filter(user=request.user).values_list('book', flat=True)
hehe = CartItem.objects.filter(user=request.user)
category = Category.objects.all()
haha = Paginator(Book, 2)
page = request.GET['page']
fianlprice = 0
for item in hehe:
fianlprice += item.book.price
# books = Book.objects.all()
books = haha(page)
return render(request, 'main/products.html', {'books':books, 'price':fianlprice, 'cart':oof, 'order':lmao, 'category':category})
products.html
<h1>Products</h1>
<h1>{{ error }}</h1>
{% if user.is_authenticated %}
<h1>Your cart currently costs ${{ price }}</h1>
{% else %}
<h1>Please login to view your cart</h1>
{% endif %}
<form method="GET" action="/search/">
<label>Choose a category</label>
<select name="category" id="category">
<option value="All" selected>All</option>
{% for name in category %}
<option value="{{ name.name }}">{{ name.name }}</option>
{% endfor %}
</select>
<input type="text" placeholder="Search here" name="search" id="search">
<button type="submit">Search</button>
</form>
{% for book in books %}
<h3>{{ book.name }}</h3>
<img src= "/media/{{ book.image }}" alt="">
<p>{{ book.description }}</p>
{% if not user.is_authenticated %}
<p>Please login</p>
{% else %}
{% if book.id in cart %}
<form method="POST" action="/removefromcartforhome/">
{% csrf_token %}
<button type="submit" name="removeid" value="{{ book.id }}">remove item from cart</button>
</form>
{% elif book.id in order %}
<h3>You already own this</h3>
{% else %}
<form method="POST" action="/addtocartforhome/">
{% csrf_token %}
<button type="submit" name="bookid" value="{{ book.id }}">Add to cart</button>
</form>
{% endif %}
{% endif %}
{% endfor %}
{% if books.has_other_pages %}
{% if listing.has_previous %}
<li class="page-item">
«
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">«</a>
</li>
{% endif %}
{% for i in books.paginator.page_range %}
{% if books.number == i %}
<li class="page-item active">
<a class="page-link">{{i}}</a>
</li>
{% else %}
<li class="page-item">
{{i}}
</li>
{% endif %}
{% endfor %}
{% if books.has_next %}
<li class="page-item">
»
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">»</a>
</li>
{% endif %}
{% else %}
{% endif %}
The request.GET['page'] will raise an error if the ?page=… querystring is missing. You can work with request.GET.get('page') to return None in case the it is missing.
Furthermore you can not call haha(page), you call the .page(…) method [Django-doc] or the .get_page(…) method [Django-doc], so:
from django.http import Http404
def showproducts(request):
oof = CartItem.objects.filter(user=request.user).values_list('book', flat=True)
lmao = OrderItem.objects.filter(user=request.user).values_list('book', flat=True)
hehe = CartItem.objects.filter(user=request.user)
category = Category.objects.all()
haha = Paginator(Book, 2)
page = request.GET.get('page') or 1 # ← 1 in case ?page=… does not exists
try:
page = int(page)
except ValueError:
raise Http404
fianlprice = 0
for item in hehe:
fianlprice += item.book.price
# books = Book.objects.all()
books = haha.page(page) # ← use the .page(…) method
return render(request, 'main/products.html', {'books':books, 'price':fianlprice, 'cart':oof, 'order':lmao, 'category':category})
So I'm trying to use pagination in django, When I click on previous it takes me to localhost:8000/thatpage/?page= when it is supposed to take me to the previous page, It also gives me error on that page. I have tried handling it in my views.py but it doesn't work.
I have also tried using books_obj.previous_page_number but that doesn't work too.
I'm using Django 3.1
views.py
def showproducts(request):
oof = CartItem.objects.filter(user=request.user).values_list('book', flat=True)
lmao = OrderItem.objects.filter(user=request.user).values_list('book', flat=True)
hehe = CartItem.objects.filter(user=request.user)
category = Category.objects.all()
haha = Paginator(Book.objects.all(), 2)
page = request.GET.get('page')
if page is None or "":
page = 1
fianlprice = 0
for item in hehe:
fianlprice += item.book.price
# books = Book.objects.all()
books = haha.page(page)
return render(request, 'main/products.html', {'books':books, 'price':fianlprice, 'cart':oof, 'order':lmao, 'category':category})
products.html
<h1>Products</h1>
<h1>{{ error }}</h1>
{% if user.is_authenticated %}
<h1>Your cart currently costs ${{ price }}</h1>
{% else %}
<h1>Please login to view your cart</h1>
{% endif %}
<form method="GET" action="/search/">
<label>Choose a category</label>
<select name="category" id="category">
<option value="All" selected>All</option>
{% for name in category %}
<option value="{{ name.name }}">{{ name.name }}</option>
{% endfor %}
</select>
<input type="text" placeholder="Search here" name="search" id="search">
<button type="submit">Search</button>
</form>
{% for book in books %}
<h3>{{ book.name }}</h3>
<img src= "/media/{{ book.image }}" alt="">
<p>{{ book.description }}</p>
{% if not user.is_authenticated %}
<p>Please login</p>
{% else %}
{% if book.id in cart %}
<form method="POST" action="/removefromcartforhome/">
{% csrf_token %}
<button type="submit" name="removeid" value="{{ book.id }}">remove item from cart</button>
</form>
{% elif book.id in order %}
<h3>You already own this</h3>
{% else %}
<form method="POST" action="/addtocartforhome/">
{% csrf_token %}
<button type="submit" name="bookid" value="{{ book.id }}">Add to cart</button>
</form>
{% endif %}
{% endif %}
{% endfor %}
{% if books.has_other_pages %}
{% if books.has_previous %}
<li class="page-item">
«
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">«</a>
</li>
{% endif %}
{% for i in books.paginator.page_range %}
{% if books.number == i %}
<li class="page-item active">
<a class="page-link">{{i}}</a>
</li>
{% else %}
<li class="page-item">
{{i}}
</li>
{% endif %}
{% endfor %}
{% if books.has_next %}
<li class="page-item">
»
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">»</a>
</li>
{% endif %}
{% else %}
<p>no pages</p>
{% endif %}
Change this code from:
if page is None or "":
page = 1
to
page = request.GET.get('page', None)
if page == None or page == "":
page = 1
# or
if not page:
page = 1
Update
You can use http referee to redirect to previous page:
if not page:
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
I am at the beginner level in the Django Framework. The queryset works well in my post list. But I face a problem in pagination to the second page queryset according to the tag field in my blog post list. When I go to the second page it shows the main list of the second page, not queryset page. I don't know where the problem behind the seen...
models.py code
class Post(models.Model):
TOPICS = (
('Programming' , 'Programming'),
('C' , 'C'),
('C++' , 'C++'),
('Java' , 'Java'),
('Python' , 'Python'),
)
title = models.CharField(max_length=100, blank=False, null=False)
content = models.TextField()
tag = models.CharField(max_length=50, choices=TOPICS, blank=False, null=False, default='Programming')
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
#redirect to post_detail when some post a blog
def get_absolute_url(self):
return reverse('post_detail', kwargs={'pk':self.pk})
viwes.py code
from django.core.paginator import Paginator
from .filters import BlogFilter
def home(request):
posts = Post.objects.all().order_by('-date_posted')
postFilter = BlogFilter(request.GET, queryset=posts)
posts = postFilter.qs
paginator = Paginator(posts, 3)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
context = {
'page_obj' : page_obj,
'postFilter' : postFilter,
}
return render(request, 'blog/index.html', context)
template code
{% extends 'blog/base.html'%}
{% load crispy_forms_tags %}
{% load static %}
{% block content %}
<section id="article">
<div class="container">
<div class="row mt-4">
<div class="col-md-8 m-auto">
<form method="get">
{{postFilter.form|crispy}}
<button class="btn btn-primary" type="submit">Search</button>
</form>
{% for i in page_obj %}
<article class="media content-section">
<a href="{% url 'user_posts' i.author.username %}">
<img class="rounded-circle article-img" src="{{i.author.profile.image.url}}" alt="">
</a>
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="{% url 'user_posts' i.author.username %}">{{ i.author }}</a>
<small class="text-muted">{{i.date_posted|date:"F d, Y"}}</small>
</div>
<h2><a class="article-title" href="{% url 'post_detail' i.id %}">{{i.title}}</a></h2>
<p class="article-content">{{i.content|linebreaks|truncatewords:50}}</p>
</div>
</article>
{% endfor %}
{% 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 %}
</div>
</div>
</div>
</section>
{% endblock %}
I'm trying to set up pagination within my Django project but I can't find a way to make the filters (ptags in my case) follow to the next pages.
Ps; I use Django-haystack with faceting for filtering.
I have a custom forms.py
from haystack.forms import FacetedSearchForm
class FacetedProductSearchForm(FacetedSearchForm):
def __init__(self, *args, **kwargs):
data = dict(kwargs.get("data", []))
self.ptag = data.get('ptags', [])
super(FacetedProductSearchForm, self).__init__(*args, **kwargs)
def search(self):
sqs = super(FacetedProductSearchForm, self).search()
if self.ptag:
query = None
for ptags in self.ptag:
if query:
query += u' OR '
else:
query = u''
query += u'"%s"' % sqs.query.clean(ptags)
sqs = sqs.narrow(u'ptags_exact:%s' % query)
return sqs
That I pass in my Views:
class FacetedSearchView(BaseFacetedSearchView):
form_class = FacetedProductSearchForm
facet_fields = ['ptags']
template_name = 'search_result.html'
paginate_by = 3
context_object_name = 'object_list'
Here's my full search_result.html:
<div>
{% if page_obj.object_list %}
<ol class="row top20" id="my_list">
{% for result in page_obj.object_list %}
<li class="list_item">
<div class="showcase col-sm-6 col-md-4">
<div class="matching_score"></div>
<a href="{{ result.object.get_absolute_url }}">
<h3>{{result.object.title}}</h3>
<h5>{{ result.object.destination }}</h5>
<img src="{{ result.object.image }}" class="img-responsive">
</a>
<div class="text-center textio">
<ul class="tagslist">
<li class="listi">
{% for tags in result.object.ptags.names %}
<span class="label label-info"># {{ tags }}</span>
{% endfor %}
</li>
</ul>
</div>
</div>
</li>
{% endfor %}
</ol>
</div>
I'm able to pass the query which is a destination to the next page by adding this at the bottom of my html:
{% if is_paginated %}
<ul class="pagination pull-right">
{% if page_obj.has_previous %}
<li><i class="fas fa-angle-left"></i>Previous page</li>
{% else %}
<li class="disabled"><span><i class="fas fa-angle-left"></i>Previous page</span></li>
{% endif %}
{% if page_obj.has_next %}
<li>See more results<i class="fas fa-angle-right"></i></li>
{% else %}
<li class="disabled"><span>See more results<i class="fas fa-angle-right"></i></span></li>
{% endif %}
</ul>
{% endif %}
{% else %}
<p> Sorry, no result found for the search term <strong>{{query}} </strong></p>
{% endif %}
But If I try to add the ptags field within the pagination code by doing &ptags like this:
{% if is_paginated %}
<ul class="pagination pull-right">
{% if page_obj.has_previous %}
<li><i class="fas fa-angle-left"></i>Previous page</li>
{% else %}
<li class="disabled"><span><i class="fas fa-angle-left"></i>Previous page</span></li>
{% endif %}
{% if page_obj.has_next %}
<li>See more results<i class="fas fa-angle-right"></i></li>
{% else %}
<li class="disabled"><span>See more results<i class="fas fa-angle-right"></i></span></li>
{% endif %}
</ul>
{% endif %}
{% else %}
<p> Sorry, no result found for the search term <strong>{{query}} </strong></p>
{% endif %}
I get this error when I go to page 2 and the selected checkboxes filters (ptags) are not following:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/search/?q=las%20vegas&ptags=&page=2
Raised by: search.views.FacetedSearchView
How can I fix this?
Your template pagination code is removing the extra filter parameters, use this -
In your my_tags.py add this template tag:
#register.simple_tag(name='url_replace')
def url_replace(request, field, value):
d = request.GET.copy()
d[field] = value
return d.urlencode()
paginate as -
{% if is_paginated %}
<br>
<div class="row">
<div class="col-12">
<ul class="pagination float-right">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?{% url_replace request 'page' page_obj.previous_page_number %}">«</a></li>
{% else %}
<li class="page-item disabled"><span class="page-link">«</span></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active page-item"><span class="page-link">{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li class="page-item"><a class="page-link" href="?{% url_replace request 'page' i %}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?{% url_replace request 'page' page_obj.next_page_number %}">»</a></li>
{% else %}
<li class="page-item disabled"><span class="page-link">»</span></li>
{% endif %}
</ul>
</div>
</div>
{% endif %}