How to do it...

Perform the following steps to create a continuously scrolling page:

  1. First, add the top movie data to your database.
A data migration is provided in the code files associated with the book that can be executed to add movie data to your project.
  1. The next step is to create a template for the list view that will also show a link to the next page, as follows:
{# templates/movies/top_movies.html #}
{% extends "base.html" %}
{% load i18n static utility_tags %}

{% block stylesheet %}
<link rel="stylesheet" type="text/css"
href="{% static 'movies/css/rating.css' %}">
<link rel="stylesheet" type="text/css"
href="{% static 'site/css/movie_list.css' %}">
{% endblock %}

{% block content %}
<h2>{% trans "Top Movies" %}</h2>
<div class="movie-list object-list">
{% trans "IMDB rating" as rating_label %}
{% for movie in object_list %}
<p class="movie item alert alert-info">
<span class="rank">{{ movie.rank }}</span>
<span class="rating"
title="{{ rating_label }}: {{ movie.rating }}">
<s style="width:{{ movie.rating_percentage }}%"></s>
</span>
<strong class="title">{{ movie.title }}</strong>
<span class="year">{{ movie.release_year }}</span>
</p>
{% endfor %}

{% if object_list.has_next %}
<p class="pagination">
<a class="next_page"
href="{% modify_query page=object_list.next_page_number %}">
{% trans "More..." %}</a>
</p>
{% endif %}
</div>
{% endblock %}

{% block extrabody %}
<script type="text/template" class="loader">
<img src="{% static 'site/img/loading.gif' %}"
alt="Loading..."></script>
{% endblock %}

{% block js %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jscroll/2.3.9/jquery.jscroll.min.js"></script>
<script src="{% static 'site/js/list.js ' %}"></script>
{% endblock %}
We use the Cloudflare CDN URL to load the jScroll plugin here, but if you opt to download a copy locally as a static file, use a {% static %} lookup to add the script to the template.
  1. The second step is to add JavaScript, as shown here:
// site_static/site/js/list.js
jQuery(function($) {
var $list = $(".object-list");
var $loader = $("script[type='text/template'].loader");

$list.jscroll({
loadingHtml: $loader.html(),
padding: 100,
pagingSelector: '.pagination',
nextSelector: 'a.next_page:last',
contentSelector: '.item,.pagination'
});
});
  1. Next, we'll add some CSS to the movies app so that ratings can be displayed using user-friendly stars instead of just numbers:
/* movies/static/movies/css/rating.css */
.rating {
color: #c90;
display: block;
margin: 0;
padding: 0;
position: relative;
white-space: nowrap;
width: 10em;
}

.rating s {
bottom: 0;
color: #fc0;
display: block;
left: 0;
overflow: hidden;
position: absolute;
top: 0;
white-space: nowrap;
}
.rating s:before,
.rating s:after {
bottom: 0;
display: block;
left: 0;
overflow: hidden;
position: absolute;
top: 0;
}

.rating s i { visibility: hidden; }

.rating:before {
content: "☆☆☆☆☆☆☆☆☆☆";
}
.rating s:after {
content: "★★★★★★★★★★";
font-size: 1.16em; /* filled stars are slightly smaller */
}
  1. We also have some site-specific styles to add to the movie list itself:
/* static/site/css/movie_list.css */
.movie { min-width: 300px; }

.movie .rank {
float: left;
margin-right: .5em;
}
.movie .rank:after { content: "." }

.movie .year:before { content: "("; }
.movie .year:after { content: ")"; }

.movie .rating {
float: right;
margin-left: 2em;
}
  1. To support all the same capabilities of the default list, but have the correct ordering and limit to only the top 250 movies, we need to add a customized view. It will also override the template to render with:
# movies/views.py
# ...
class TopMoviesView(MovieListView):
template_name = "movies/top_movies.html"

def get_queryset_and_facets(self, form):
qs, facets = super().get_queryset_and_facets(form)
qs = qs.order_by("rank")
qs.filter(rank__gte=1, rank__lte=250)
return qs, facets
  1. And, finally, let's add a new URL rule to the top 250 listing:
# movies/urls.py
from django.urls import path

from .views import MovieListView, TopMoviesView

urlpatterns = [
# ...
path('top/', TopMoviesView.as_view(), name='top-movies'),
]
Remember to pull the new static file into the static directory. This can be done by  copying the files over manually, but this is made easier with the collectstatic management command. Just be careful you have made edits directly under your project's static folder, as those will be overwritten.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.51.36