python - django blog archive, index and pagination comprehensive application -
background:
i building blog research group. on publication archive page going display publications of mentor. here there side column show archive index allowing users view publications year. , @ bottom of page there django paginator separate publications in several pages 7 publications per page.
problem:
when pagination used, publications divided list, {{publication.published_time}} contain data in current page rather whole dataset. thus, wrote hard code of year information in front end , add url corresponding year. apparently, wish can distinct year information publications on basis of existence of paginator. besides, transfer year value directly in url.
code:
url.py
:
url(r'^publications/(?p<year>[0-9]{4})/$', publicationyeararchiveview.as_view(), name="publication_year_archive"),
views.py
:
class publicationyeararchiveview(yeararchiveview): queryset = publication.objects.all() date_field = "published_time" make_object_list = true allow_future = true def listing(request): limit = 7 publication_list = publication.objects.all().order_by('published_time').reverse() paginator = paginator(publication_list, limit) # show 7 publications per page page = request.get.get('page') try: publications = paginator.page(page) except pagenotaninteger: # if page not integer, deliver first page. publications = paginator.page(1) except emptypage: # if page out of range (e.g. 9999), deliver last page of results. publications = paginator.page(paginator.num_pages) return render(request, 'research_spacecoupe/research_accomplishment.html', {'publications': publications})
research.html
:
paginator:
{% if publications.has_other_pages %} <ul class="pagination"> {% if publications.has_previous %} <li><a href="?page={{ publications.previous_page_number }}">«</a></li> {% else %} <li class="disabled"><span>«</span></li> {% endif %} {% in publications.paginator.page_range %} {% if publications.number == %} <li class="active"><span>{{ }} <span class="sr-only">(current)</span></span></li> {% else %} <li><a href="?page={{ }}">{{ }}</a></li> {% endif %} {% endfor %} {% if publications.has_next %} <li><a href="?page={{ publications.next_page_number }}">»</a></li> {% else %} <li class="disabled"><span>»</span></li> {% endif %} </ul> {% endif %}
index:
<div class="sidebar-module"> <h4>archive</h4> <ol class="list-unstyled"> <li><a href="{% url 'research_spacecoupe:listing' %}">all publications</a></li> <li><a href="{% url 'research_spacecoupe:publication_year_archive' 2017 %}">2017</a></li> <li><a href="{% url 'research_spacecoupe:publication_year_archive' 2016 %}">2016</a></li> <li><a href="{% url 'research_spacecoupe:publication_year_archive' 2015 %}">2015</a></li> <li><a href="{% url 'research_spacecoupe:publication_year_archive' 2013 %}">2013</a></li> <li><a href="{% url 'research_spacecoupe:publication_year_archive' 2012 %}">2012</a></li> <li><a href="{% url 'research_spacecoupe:publication_year_archive' 2011 %}">2011</a></li> <li><a href="{% url 'research_spacecoupe:publication_year_archive' 2010 %}">2010</a></li> </ol> </div>
note:
now page work, , can click year(eg. 2017) view publications published in 2017 , click "all publications" view publications. want replace number of year distinct publications.published_time of publications. can fix it?
views.py
:
from django.db.models.functions import extractyear django.db.models import count # ... def listing(request): # ... years = publication.objects \ .annotate(year=extractyear('published_time')) \ .values_list('year') \ .annotate(count=count('id')) \ .values_list('year', flat=true) \ .order_by('year') return render(request, 'research_spacecoupe/research_accomplishment.html', {'publications': publications, 'years': years})
research_accomplishment.html
:
<div class="sidebar-module"> <h4>archive</h4> <ol class="list-unstyled"> <li><a href="{% url 'research_spacecoupe:listing' %}">all publications</a></li> {% year in years %} <li><a href="{% url 'research_spacecoupe:publication_year_archive' year %}">{{ year }}</a></li> {% endfor %} </ol> </div>
Comments
Post a Comment