I’m trying to create a next and previous button on a single(detail) page. I don’t understand how to make this happen. These are my codes
views.py
def playsong(request, id,slug, tag_slug=None):
beat = Number_beat.objects.get(created_beats=id)
album = Album.objects.get(album_id= id)
albu = Album_beats.objects.filter(album_name= id)
bea = Beats.objects.all()
nextbeat = Number_beat.objects.filter(id__gt = beat.id).order_by(‘id’).first() #.values(‘id’)[:1]
lastbeat = Number_beat.objects.filter(id__lt= beat.id).order_by(‘id’).last()
tag = None
if tag_slug:
tag = get_object_or_404(Tag,slug=tag_slug)
beat=beat.filter(tags__in=[tag])
context={
'beat':beat,
'nextbeat':nextbeat,
'lastbeat':lastbeat,
'album':album,
'albu':albu, }
return render(request, 'music/playsong.html', context)
html
{% if lastbeat %}
<li class="page-item">
<a class="page-link" href="{% url 'playsong' lastbeat %}?{{ request.GET.urlencode }}" id="pred">{{ lastbeat }}</a>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="{% url 'index' %}" id="pred">home</a>
</li>
{% endif %}
{% if nextbeat %}
<li class="page-item">
<a class="page-link" href="{% url 'playsong' %}?next={{ nextbeat|urlencode }}" id="predj">{{nextbeat}}</a>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="{% url 'index' %}" id="pred">home</a>
</li>
{% endif %}
please can anyone help on can I do
Related
class ProductList(ListView):
model = Product
paginate_by = 8
def company_page(request, slug):
...
product_list = Product.objects.filter(company=company).order_by('-pk')
paginator = Paginator(product_list, 4)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
return render(request, 'product/product_list.html', {
...,
'product_list': product_list,
'page_obj': page_obj
})
views.py
<nav aria-label="Pagination">
<ul class="pagination justify-content-center my-5">
{% if page_obj.has_previous %}
<li class="page-item mx-auto lead">
<a class="page-link" href="?page={{page_obj.previous_page_number}}" tabindex="-1" aria-disabled="true">
Newer</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1" aria-disabled="true">
Newer</a>
</li>
{% endif %}
{% if page_obj.has_next %}
<li class="page-item mx-auto lead">
<a class="page-link" href="?page={{page_obj.next_page_number}}">
Older</a>
</li>
{% else %}
<li class="page-item disabled mx-auto lead">
<a class="page-link" href="#!">
Older</a>
</li>
{% endif %}
</ul>
</nav>
product_list.html
I added pagination on ProductList view with paginated_by and imported Paginator to make other pages using a function view but it's only paginated on ProductList view and doesn't work on company_page view. The Newer & Older buttons work but the page keeps showing every product_list objects. How can I make it work on all pages?
Try this:
views.py
# other views ...
def company_page(request, slug):
product_list = Product.objects.filter(company=company).order_by('-pk')
page = request.GET.get('page', 1)
paginator = Paginator(product_list, 4)
try:
Products = paginator.page(page)
except PageNotAnInteger:
Products = paginator.page(1)
except EmptyPage:
Products = paginator.page(paginator.num_pages)
context = {'Products': Products}
return render(request, 'product/product_list.html', context)
product_list.html
<nav aria-label="Pagination">
{% if Products.has_other_pages %}
<ul class="pagination justify-content-center my-5">
{% if Products.has_previous %}
<li class="page-item mx-auto lead">
<a class="page-link" href="?page={{ Products.previous_page_number }}" tabindex="-1" aria-disabled="true">
Previous</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1" aria-disabled="true">
Previous</a>
</li>
{% endif %}
{% for i in Products.paginator.page_range %}
{% if Products.number == i %}
<li class="page-item mx-auto lead">
<a class="page-link" href="?page={{ i }}">
{{ i }}</a>
</li>
{% endif %}
{% endfor %}
{% if Products.has_next %}
<li class="page-item mx-auto lead">
<a class="page-link" href="?page={{ Products.next_page_number }}">
Next</a>
</li>
{% else %}
<li class="page-item disabled mx-auto lead">
<a class="page-link" href="#!">
Next</a>
</li>
{% endif %}
</ul>
{% endif %}
</nav>
I am facing this probelm in my e-commerce project when i try to pagination
Cannot resolve keyword '' into field. Choices are: created_at, description, id, is_active, subcategories, thumbnail, title, url_slug
# CATEGORIES
class CategoriesListView(ListView):
model = Categories
template_name = "admin_local/category_list.html"
paginate_by = 3
def get_queryset(self):
filter_val = self.request.GET.get("filter", "")`enter code here`
order_by = self.request.GET.get("orderby", "id")
if filter_val != "":
cat = Categories.objects.filter(
Q(title__contains=filter_val) | Q(description__contains=filter_val)).order_by(order_by)
else:
cat = Categories.objects.all().order_by(order_by)
return cat
def get_context_data(self, **kwargs):
context = super(CategoriesListView, self).get_context_data(**kwargs)
context["filter"] = self.request.GET.get("filter", "")
context["orderby"] = self.request.GET.get("orderby", "")
context["all_table_fields"] = Categories._meta.get_fields()
return context
I am facing this probelm in my e-commerce project when i try to pagination
This Is html file
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-body">
<nav aria-label="Page navigation example">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link"
href="{% url 'category_list' %}?filter={{ filter }}&orderby={{ orderby }}&page={{ page_obj.previous_page_number }} ">Previous</a>
</li>
{% else %}
<li class="page-item disabled"><a class="page-link"
href="#">Previous</a>
</li>
{% endif %}
<li class="page-item"><a class="page-link" href="#">1</a></li>
<li class="page-item"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link"
href="{% url 'category_list' %}?filter={{ filter }}&orderby={{ orderby }}&page={{ page_obj.next_page_number }} ">Next</a>
</li>
{% else %}
<li class="page-item disabled"><a class="page-link"
href="#">Previous</a>
</li>
{% endif %}
</ul>
</nav>
</div>
</div>
</div>
</div>
The reason you get this is because you use orderby={{ orderby }} which is empty if it is not filled in.
You should pass the context with:
context['orderby'] = self.request.GET.get('orderby', 'id')
furthermore you should |urlencode the parameters:
<a class="page-link" href="{% url 'category_list' %}?filter={{ filter|urlencode }}&orderby={{ orderby|urlencode }}&page={{ page_obj.next_page_number }} ">Next</a>
You might also want to avoid ordering by an arbitrary field, since ordering can expose sensitive information, for example trying to order by the (hashed) password of a user.
implemented sorting and pagination on the product page, but sorting gets confused when moving to the next page. How do I apply a filter to all pagination pages?and i got smth weird with dropdown selection: after moving to next pages it starts act like crazy,after choosing a filter and refreshing the page, it returns to the default value and seems to be changing places (exmp: $$-$ -> $-$$ and vice versa , and i must guess who is who :) )
template
<div class="product-sorting d-flex">
<p>Sort by:</p>
<form name="selectForm" action="{% url 'shop' %}" method="get">
<label for="orderby"></label>
<select name="orderby" id="orderby" onchange="selectForm.submit();">
<option value="price">price: $$ - $</option>
<option value="-price">price: $ - $$</option>
</select>
<input type="submit" class="d-none" value="submit">
</form>
</div>
views
class Shop(ListView):
template_name = 'essense/shop.html'
context_object_name = 'items'
paginate_by = 9
allow_empty = False
model = Item
def get_context_data(self, *, object_list=None, **kwargs):
***context***
def get_ordering(self):
return self.request.GET.get('orderby', )
pagination
{% if page_obj.has_other_pages %}
</div>
<nav aria-label="navigation">
<ul class="pagination mt-50 mb-70">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link"
href="?page={{ page_obj.previous_page_number }}"><i
class="fa fa-angle-left"></i></a>
</li>
{% endif %}
{% for p in page_obj.paginator.page_range %}
{% if page_obj.number == p %}
<li class="page-item"><a class="page-link" href="#">{{ p }}</a></li>
{% elif p > page_obj.number|add:-3 and p < page_obj.number|add:+3 %}
<li class="page-item"><a class="page-link" href="?page={{ p }}">{{ p }}</a>
</li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link"
href="?page={{ page_obj.next_page_number }}"><i
class="fa fa-angle-right"></i></a></li>
{% endif %}
</ul>
</nav>
</div>
</div>
{% endif %}
You are only passing the new page number with the pagination link, so any other parameters that may have been involved in filtering will get lost on the new page.
You need a way to maintain the extra GET params. One way to do this might be a template tag that you can pass the new page number to;
project/templatetags/pagination_tags.py:
from django import template
register = template.Library()
#register.simple_tag
def url_replace(request, field, value):
d = request.GET.copy()
d[field] = value
return d.urlencode()
Then in your template the next (and previous) links would use that tag:
{% load pagination_tags %}
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="?{% url_replace request 'page' page_obj.next_page_number %}">
<i class="fa fa-angle-right"></i>
</a>
</li>
{% endif %}
I would like to make a header that is dynamic depending if a user is logged in, I have a custom login to active directory. logged in a parameter in a cookie, and I use the .get in python to fetch it.
I keep looking and not finding how to do this. Any help would be appreciated.
I always get index.html no matter what, and I'm sure I have the cookie.
{% if logged == 1 %}
{% extends "logged-index.html" %}
{% else %}
{% extends "index.html" %}
{% endif %}
If you want a dynamic header/navbar or footer; couldn't you just put if/else statements into your main template that you extend from, so that different links and things show depending on whether the user is logged in or not. This is what I did on my web app:
<ul class="navbar-nav">
{% if current_user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="{{ url_for('account') }}">Account</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('logout') }}">Logout {{current_user.username}}?</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{{ url_for('logIn') }}">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('signUp') }}">Sign Up</a>
</li>
{% endif %}
Perhaps I do not understand what you are after, but:
I think that it would be the same logic as this dynamic title logic:
<head>
{% if logged == 1 %}
<title>{{ username }} - is logged in</title>
{% else %}
<title>Welcome to My Website!</title>
{% endif %}
</head>
I borrowed the concept from:
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ii-templates
The cart icon on my navbar diasappears once the user logs in. I have tried to debug it but i cannot figure it out. What am I doing wrong? Do I need to authenticate the user similar to the login icon in the toolbar?
<ul class="navbar-nav ml-auto">
{% if user.is_authenticated %}
<li
{% if 'dashboard' in request.path %}
class="nav-item active mr-3"
{% else %}
class="nav-item mr-3"
{% endif %}
>
<a class="nav-link" href="{% url 'profile' %}">
Welcome {{ user.username }}</a>
</li>
<li class="nav-item mr-3">
<a href="javascript:{document.getElementById('logout').submit()}" class="nav-link">
<i class="fas fa-sign-out-alt"></i> Logout
</a>
<form action="{% url 'logout' %}" method="POST" id="logout">
{% csrf_token %}
<input type="hidden">
</form>
</li>
{% else %}
<li
{% if 'register' in request.path %}
class="nav-item active mr-3"
{% else %}
class="nav-item mr-3"
{% endif %}
>
<a class="nav-link" href="{% url 'register' %}">
<i class="fas fa-user-plus"></i> Register</a>
</li>
<li
{% if 'login' in request.path %}
class="nav-item active mr-3"
{% else %}
class="nav-item mr-3"
{% endif %}
>
<a class="nav-link" href="{% url 'login' %}">
<i class="fas fa-sign-in-alt"></i>
Login</a>
<li
{% if 'cart' in request.path %}
class="nav-item active mr-3"
{% else %}
class="nav-item mr-3"
{% endif %}
>
<a class="nav-link" href="{% url 'cart' %}">
<i class="fas fa-cart-plus"></i>
Cart</a>
</li>
Before I login
After Login
It looks like your Cart item is in the else block of {% if user.is_authenticated %}, so if the user is not authenticated it will display, but if they are it won't.
If you add an {% endif %} above the cart link it should work, and it will display when they are both logged in and logged out. See below:
<ul class="navbar-nav ml-auto">
{% if user.is_authenticated %}
<li
{% if 'dashboard' in request.path %}
class="nav-item active mr-3"
{% else %}
class="nav-item mr-3"
{% endif %}
>
<a class="nav-link" href="{% url 'profile' %}">
Welcome {{ user.username }}</a>
</li>
<li class="nav-item mr-3">
<a href="javascript:{document.getElementById('logout').submit()}" class="nav-link">
<i class="fas fa-sign-out-alt"></i> Logout
</a>
<form action="{% url 'logout' %}" method="POST" id="logout">
{% csrf_token %}
<input type="hidden">
</form>
</li>
{% else %}
<li
{% if 'register' in request.path %}
class="nav-item active mr-3"
{% else %}
class="nav-item mr-3"
{% endif %}
>
<a class="nav-link" href="{% url 'register' %}">
<i class="fas fa-user-plus"></i> Register</a>
</li>
<li
{% if 'login' in request.path %}
class="nav-item active mr-3"
{% else %}
class="nav-item mr-3"
{% endif %}
>
<a class="nav-link" href="{% url 'login' %}">
<i class="fas fa-sign-in-alt"></i>
Login</a>
{% endif %} <!-- Put endif here -->
<li
{% if 'cart' in request.path %}
class="nav-item active mr-3"
{% else %}
class="nav-item mr-3"
{% endif %}
>
<a class="nav-link" href="{% url 'cart' %}">
<i class="fas fa-cart-plus"></i>
Cart</a>
</li>