django sorting table with pagination - django

I want to sort my table columns both ways (ascending and descending, switch upon pressing a button). The problem I have is my tables go out of order when I switch a page.
views.py
def company_index(request):
order_by = request.GET.get('order_by')
companies = Company.objects.all().order_by(Lower(order_by))
paginator = Paginator(companies, 10)
page = request.GET.get('page')
try:
all_companies = paginator.page(page)
except PageNotAnInteger:
all_companies = paginator.page(1)
except EmptyPage:
all_companies = paginator.page(paginator.num_pages)
return render(request, 'companies/company_index.html',
{'all_companies': all_companies})
Here is how I display data in my templates (I shortened class names for better post visibility):
<thead>
<tr>
<th>Company name <a class="glyphicon" href="?order_by=company_name"></a></th>
<th>Company address <a class="glyphicon" href="?order_by=company_address"></a></th>
<th>Tax ID <a class="glyphicon" href="?order_by=tax_id"></a></th>
<th>Company ID <a class="glyphicon" href="?order_by=company_id"></a></th>
<th>KRS Number <a class="glyphicon" href="?order_by=KRS_number"></a></th>
</tr>
</thead>
My pagination code:
<ul class="pagination">
{% if all_companies.has_previous %}
<li>previous</li>
{% endif %}
<li class="disabled"><a>Page {{ all_companies.number }} of {{ all_companies.paginator.num_pages }}</a></li>
{% if all_companies.has_next %}
<li>next</li>
{% endif %}
</ul>
When I switch to other page {{order_by}} passes None. Also, how can I make it sort descending or ascending, after pressing a button?
I want to do it without outside apps or libraries, to have a better understanding of django.

You forgot to add order_by in the context:
return render(request, 'companies/company_index.html',
{'all_companies': all_companies, 'order_by': order_by})
Ascending/descending in template:
company name ascending
company name descending
...
previous
...
next
In the view:
def company_index(request):
order_by = request.GET.get('order_by')
direction = request.GET.get('direction')
ordering = Lower(order_by)
if direction == 'desc':
ordering = '-{}'.format(ordering)
companies = Company.objects.all().order_by(ordering)
paginator = Paginator(companies, 10)
page = request.GET.get('page')
try:
all_companies = paginator.page(page)
except PageNotAnInteger:
all_companies = paginator.page(1)
except EmptyPage:
all_companies = paginator.page(paginator.num_pages)
return render(request, 'companies/company_index.html',
{'all_companies': all_companies,
'order_by': order_by, 'direction': direction})

Related

How can I Render Data To Main Template In Other Extended View From Main Template?

First of all, hello everyone. I apologize to everyone for my bad English, I will try my best. I am making a video site, in this site there are three main views, they are single-video-view, index-view and main-view. All other views extends from main-view (%extends 'base/main.html' %). There is a sidebar that should appear on all pages. In this sidebar, the categories of pre-loaded videos should be listed.
My urls.py:
urlpatterns = [
path('',views.index,name="index"),
path('main/',views.main,name="main"),
path('video/<slug:slug>/',views.video,name="video"),
#path('test/',views.category,name="setcategory"),
path('apivideo/',views.apivideo,name="apivideo"),
]
If I visit main.html directly working fine
Single video view, categories not listing
Index view, categories not listing
View.py:
def index(request):
# q = request.GET.get('q')
# if request.GET.get('q') != None:
if 'q' in request.GET and request.GET['q']:
page = request.GET.get('page', 1)
q = request.GET['q']
videos = Video.objects.filter(
Q(video_title__icontains=q) | Q(video_tags__icontains=q))
paginator = Paginator(videos, 24)
videos = paginator.page(page)
return render(request, 'base/index.html', {'videos': videos, 'q': q})
else:
main(request)
videos = Video.objects.all().order_by("-id")
paginator = Paginator(videos, 24)
page_number = request.GET.get('page')
videos = paginator.get_page(page_number)
return render(request, 'base/index.html', {'videos': videos})
def main(request):
setcategory = []
category = Video.objects.values_list('video_tags', flat=True)
for i in range(0, len(category)):
for x in range(0, len(category[i])):
categorycount = Video.objects.filter(Q(video_title__icontains=str(
category[i][x]).lower()) | Q(video_tags__icontains=str(category[i][x]).lower())).count()
if (categorycount >= 10):
print("if: " + str(categorycount))
setcategory.append(str(category[i][x]).lower())
else:
print("else: " + str(categorycount))
break
setcategory = sorted([*set(setcategory)])
return render(request, 'base/main.html', {'setcategory': setcategory})
def video(request, slug):
eachvideo = Video.objects.get(video_slug=slug)
randomVideo = list(Video.objects.all())
randomVideo = random.sample(randomVideo, 20)
return render(request, 'base/video.html', {'eachvideo': eachvideo, 'randomVideo': randomVideo})
Main.html:
Some HTML..
<ul class="sidebar navbar-nav toggled">
{% if setcategory %}
{% for category in setcategory %}
<li class="nav-item active">
<a class="nav-link" href="{% url 'index' %}?q={{category}}">
<i class="fas fa-fw fa-home"></i>
<span>{{category|title}}</span>
</a>
</li>
{% endfor %}
{% endif %}
</ul>
Some HTML..
The code I wrote for this works fine, but it only works when I add a redirect to the main-view specifically in the url .py file and go to the main view page. What I want is for these categories to appear in the sidebar when the user browses the index or other pages.

Pagination Based on Queryset

I'm trying to implement pagination on my Django app that is based on the filtered queries, but the pagination shows all the objects even the ones not filtered, any insight on what I'm doing wrong?
Any assistance would be appreciated.
def searchPropertyListView(request):
city = City.objects.all().annotate(
num_property=Count("property")).order_by("-num_property")
categories = Category.objects.all()
purposes = Purpose.objects.all()
featured = list(Property.objects.filter(featured=True))
shuffle(featured)
querySet = Property.objects.all()
city_or_neighborhood = request.GET.get('city_or_neighborhood')
category = request.GET.get('category')
purpose = request.GET.get('purpose')
if city_or_neighborhood != '' and city_or_neighborhood is not None:
querySet = querySet.filter(Q(city__title__icontains=city_or_neighborhood)
| Q(neighborhood__title__icontains=city_or_neighborhood)
).distinct()
if category != '' and category is not None:
querySet = querySet.filter(category__title=category)
if purpose != '' and purpose is not None:
querySet = querySet.filter(purpose__title=purpose)
paginator = Paginator(querySet, 1)
page = request.GET.get('page')
try:
querySet = paginator.page(page)
except PageNotAnInteger:
querySet = paginator.page(1)
except EmptyPage:
querySet = paginator.page(paginator.num_pages)
Paginator.get_page is New since Django 2.0.
def searchPropertyListView(request):
...
Your code here
...
paginator = Paginator(querySet, 1)
page = request.GET.get('page')
page_obj = paginator.get_page(page)
And html
{% for property in page_obj %}
{{ property.city.title }}<br>
...
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
next
last »
{% endif %}
</span>
</div>
See Using Paginator in a view function

Reversing results for current page (django paginator generic view)

I'm using the standard django paginator in my generic view like this:
def get_context_data(self, **kwargs):
context = super(ArchivePagedView, self).get_context_data(**kwargs)
article_list = Article.published
#=====================================
paginator = Paginator(article_list, self.paginate_by)
page = self.request.GET.get('page')
try:
article_list = paginator.page(page)
except PageNotAnInteger:
article_list = paginator.page(1)
except EmptyPage:
article_list = paginator.page(paginator.num_pages)
if 'reverse' in self.request.GET:
article_list = article_list.reverse() #This doesn't work!
else:
article_list = article_list.all()
context['article_list'] = article_list
return context
As you can see I want to override article_list with the same list, but in reversed direction, if reverse is in the URL in behind the question mark. That information I get by 'reverse' in self.request.GET.
But I get an error: AttributeError: 'Page' object has no attribute 'reverse'. How do I reverse this? (I don't want to have duplicated code in my template.)
Before I fixed this by making an extra context variable (context['reverse']) which says whether the list should be reversed or not, and then I used duplicated code like this:
{% if reverse %}
{% for article in article_list reversed %}
... some code
{% endfor %}
{% else %}
{% for article in article_list %}
... the same code
{% endfor %}
{% endif %}
I wonder if there was no better solution.
Try this
article_list.object_list = list(reversed(article_list.object_list))

Pagination in Wagtail

I'm fairly new to Wagtail, and I am in the process of creating a site that will have a Resources (blog) section and I'm not sure how to implement pagination so that there are only 5 posts on each page and the user has to click a number (1, 2, 3, etc.) to go to the next page to see the next 5 posts.
I have this in my template for the pagination section of the resource/blog index page:
<ul class="pagination">
<li><i class="fa fa-angle-left"></i></li>
<li class="active">1</li>
<li>2</li>
<li>3</li>
<li><i class="fa fa-angle-right"></i></li>
</ul>
What code do I need to incorporate to make this functional? Thanks in advance.
Django provides the module django.core.paginator for this purpose: https://docs.djangoproject.com/en/1.10/topics/pagination/ . Using this within Wagtail is very similar to the examples in the Django documentation - the only real difference is that when you're setting up the Paginator object to be passed to the template, you do that with a get_context method on the page model, instead of a view function. Your model definition will look something like this:
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
class ResourceIndexPage(Page):
# ...
def get_context(self, request):
context = super(ResourceIndexPage, self).get_context(request)
# Get the full unpaginated listing of resource pages as a queryset -
# replace this with your own query as appropriate
all_resources = ResourcePage.objects.live()
paginator = Paginator(all_resources, 5) # Show 5 resources per page
page = request.GET.get('page')
try:
resources = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
resources = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
resources = paginator.page(paginator.num_pages)
# make the variable 'resources' available on the template
context['resources'] = resources
return context
Within your template, you can now loop over the items using {% for resource in resources %}, and display the pagination links as follows:
<ul class="pagination">
{% if resources.has_previous %}
<li><i class="fa fa-angle-left"></i></li>
{% endif %}
{% for page_num in resources.paginator.page_range %}
<li {% if page_num == resources.number %}class="active"{% endif %}>{{ page_num }}</li>
{% endfor %}
{% if resources.has_next %}
<li><i class="fa fa-angle-right"></i></li>
{% endif %}
</ul>
I very much appreciate that you got me here - thanks so much for the assist. I had to make some adjustments to make it work. Here's the model if anyone comes across the same issue:
class NewsIndexPage(Page):
intro = RichTextField(blank=True)
def get_context(self, request):
context = super(NewsIndexPage, self).get_context(request)
# Get the full unpaginated listing of resource pages as a queryset -
# replace this with your own query as appropriate
blogpages = self.get_children().live().order_by('-first_published_at')
paginator = Paginator(blogpages, 3) # Show 3 resources per page
page = request.GET.get('page')
try:
blogpages = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
blogpages = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
blogpages = paginator.page(paginator.num_pages)
# make the variable 'resources' available on the template
context['blogpages'] = blogpages
return context
...and here's the HTML:
<ul class="pagination">
{% if blogpages.has_previous %}
<li>
<i class="fa fa-angle-left"></i>
</li>
{% endif %}
{% for page_num in blogpages.paginator.page_range %}
<li {% if page_num == blogpages.number %} class="active"{% endif %}>
{{ page_num }}
</li>
{% endfor %}
{% if resources.has_next %}
<li>
<i class="fa fa-angle-right"></i>
</li>
{% endif %}
</ul>
It works like a charm - and adds to the learning curve!
In case it's useful to anyone, I wanted this to work as closely as possible to the class-based view ListView, and so I ended up with this:
from django.core.paginator import Paginator, InvalidPage
from django.http import Http404
from django.utils.translation import gettext as _
from wagtail.core.models import Page
class ArticleListPage(Page):
# Some Page variables set here. #
# Pagination variables:
paginator_class = Paginator
paginate_by = 10
page_kwarg = 'page'
paginate_orphans = 0
allow_empty = False
def get_context(self, request):
context = super().get_context(request)
queryset = Page.objects.live()
paginator, page, queryset, is_paginated = self.paginate_queryset(
queryset, self.paginate_by, request)
context.update({
'paginator': paginator,
'page_obj': page,
'is_paginated': is_paginated,
'object_list': queryset,
})
return context
def paginate_queryset(self, queryset, page_size, request):
"""
Adapted from the ListView class-based view.
Added the request argument.
"""
paginator = self.paginator_class(
queryset,
self.paginate_by,
orphans=self.paginate_orphans,
allow_empty_first_page=self.allow_empty)
page_kwarg = self.page_kwarg
page = request.GET.get(page_kwarg) or 1
try:
page_number = int(page)
except ValueError:
if page == 'last':
page_number = paginator.num_pages
else:
raise Http404(_("Page is not 'last', nor can it be converted to an int."))
try:
page = paginator.page(page_number)
return (paginator, page, page.object_list, page.has_other_pages())
except InvalidPage as e:
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
'page_number': page_number,
'message': str(e)
})
This will give you the same paginator, page_obj, is_paginated and object_list variables in your template that you would get with a normal Django ListView.
(Using python 3, Django 2.1 and Wagtail 2.3.)

How to implement Post/Redirect/Get in django pagination?

I have a view that filters out results for a posted search form:
def profile_advanced_search(request):
args = {}
if request.method == "POST":
form = AdvancedSearchForm(request.POST)
qs=[]
if form.is_valid():
cd = form.cleaned_data
s_country=cd['country']
s_province=cd['province']
s_city = cd['city']
if s_country: qs.append(Q(country__icontains = s_country))
if s_province: qs.append( Q(province__icontains=s_province))
if s_city: qs.append( Q(city__icontains=s_city))
f = None
for q in qs:
if f is None:
f=q
else: f &=q
list = UserProfile.objects.filter(f).order_by('-created_at')
else:
form = AdvancedSearchForm()
list = UserProfile.objects.all().order_by('-created_at')
paginator = Paginator(list,10)
page= request.GET.get('page')
try:
results = paginator.page(page)
except PageNotAnInteger:
results = paginator.page(1)
except EmptyPage:
results = paginator.page(paginator.num_pages)
args.update(csrf(request))
args['form'] = form
args['results'] = results
return render_to_response('userprofile/advanced_search.html', args,
context_instance=RequestContext(request))
the urls.py part is:
url(r'^search/$', 'userprofile.views.profile_advanced_search'),
The template is:
<form action="/search/" method="post">{% csrf_token %}
<ul class="list-unstyled">
<li><h3>Country</h3></li>
<li>{{form.country}}</li><br>
<h4>Province</h4>
<li>{{form.province}}</li>
<h4>City</h4>
<li>{{form.city}}</li>
</ul>
<input type="submit" name="submit" value="search" />
</form>
Search Results:
{% for p in results %}
<div">
<div>
<br>
<strong><a href="/profile/{{p.username}}" >{{p.username}}</a></strong>
{{p.country}} <br>
{{p.province}} <br>
{{p.city}} <br>
</div>
</div>
{% endfor %}
<div>
<div class="pagination">
{% if results.has_previous %}
<< Prev &nbsp
{% endif %}
{% if results.has_next %}
Next >>
{% endif %}
</div>
</div>
</div>
These work fine for the first page, but to deal with the later pages, it is suggested that I need to implement Post/Redirect/Get .
However I have had difficulty to make such views/template/urls to deal with GET pages regarding that all of the search parameters are arbitrary. So I appreciate a complete solution.
Do you need 2 views. First one for form search and second one to show results. You have not implemented redirect in any way in your sample code!
urls
...
url(r'^search/$',
'userprofile.views.profile_advanced_search'),
url(r'^show/(?P<country>\w+)/(?P<province>\w+)/(?P<site>\w+)/(?P<page>\d+)',
'userprofile.views.profile_advanced_show'),
...
profile_advanced_search
def profile_advanced_search(request):
args = {}
if request.method == "POST":
form = AdvancedSearchForm(request.POST)
qs=[]
if form.is_valid():
cd = form.cleaned_data
s_country=cd['country']
s_province=cd['province']
s_city = cd['city']
return HttpResponseRedirect(
reverse('userprofile.views.profile_advanced_show',
args=(s_country, s_province, s_city, 0, )))
return HttpResponseRedirect(
reverse('userprofile.views.profile_advanced_show',
args=('+', '+', '+', 0, )))
profile_advanced_show
def profile_advanced_show(request, s_country='',
s_province='', s_city='', page=0):
f = some filters with s_country, s_province and s_city
list = UserProfile.objects.filter(f).order_by('-created_at')
paginator = Paginator(list,10)
try:
results = paginator.page(page)
except PageNotAnInteger:
results = paginator.page(1)
except EmptyPage:
results = paginator.page(paginator.num_pages)
args.update(csrf(request))
form = AdvancedSearchForm(initial={ 's_country': s_country, ... } )
args['form'] = form
args['results'] = results
return render_to_response('userprofile/advanced_search.html', args,
context_instance=RequestContext(request))
Notice: improve it for not valid form submissions. Remember you can send parameters to second view via GET as key value instead route values.