Home > OS >  How do I add Page in this my code? django
How do I add Page in this my code? django

Time:07-25

I want to add paging function in my code. This socre_by function line up by score. But A lot of Movie and TV line up. I want to page Movie and TV. How can I add paging functionality while preserving the functionality of this code?

def Score_by(request):
    query_min = 0
    query_max = 10
    if request.GET.get('min') and request.GET.get('max'):
        query_min = request.GET.get('min')
        query_max = request.GET.get('max')
    movies = Movie.objects.order_by('-stars')
    movie_list= []
    Ranking = 1
    if movies.exists():
        for obj in movies:
            if (float(query_min) <= float(obj.average_stars())) and (float(query_max) >= float(obj.average_stars())):
                data = requests.get(f"https://api.themoviedb.org/3/movie/{obj.id}?api_key={TMDB_API_KEY}&language=en-US")
                data_movie = data.json()
                data_movie['score'] = obj.average_stars()
                data_movie['Ranking'] = Ranking
                movie_list.append(data_movie)
                Ranking  = 1
           # print(movie_list)
    tv = TV.objects.order_by('-stars')
    tv_list = []
    Ranking = 1
    if tv.exists():
        for obj in tv:
            if (float(query_min) <= float(obj.average_stars())) and (float(query_max) >= float(obj.average_stars())):
                data = requests.get(f"https://api.themoviedb.org/3/tv/{obj.id}?api_key={TMDB_API_KEY}&language=en-US")
                data_tv = data.json()
                data_tv['score'] = obj.stars
                data_movie['Ranking'] = Ranking
                tv_list.append(data_tv)
                Ranking  = 1
    context = {
        'movie':movie_list,
        'tv'   :tv_list
    }
    return render(request, 'Movie/score_by.html', context)

CodePudding user response:

Use a paginator.

from django.core.paginator import Paginator
paginator = Paginator(movie_list, 15) # or tv_list
  • Related