main
syk1209wwz0306 6 months ago
parent 4903aa1da5
commit 1ab05b90f4

@ -13,22 +13,10 @@ urlpatterns = [
# 电影详情页面 # 电影详情页面
path('detail/<int:pk>', views.MovieDetailView.as_view(), name='detail'), path('detail/<int:pk>', views.MovieDetailView.as_view(), name='detail'),
# 搜索功能
path('search', views.SearchView.as_view(), name='search'),
] ]
'''# 搜索功能
#path('search', views.SearchView.as_view(), name='search'),
# 热门电影
path('hot', views.PopularMovieView.as_view(), name='hot'),
# 登录
path('login', views.LoginView.as_view(), name='login'),
# 退出
path('logout', views.UserLogout, name='logout'),
# 评分历史页面
path('history/<int:pk>', views.RatingHistoryView.as_view(), name='history'),
# 删除记录
path('del_rec/<int:pk>', views.delete_recode, name='delete_record'),
# 推荐页面
path('recommend', views.RecommendMovieView.as_view(), name='recommend')'''

@ -1,11 +1,6 @@
import os.path import os.path
from django.contrib import messages
from django.db.models import Max, Count
from django.shortcuts import render, redirect, reverse
from django.views.generic import View, ListView, DetailView from django.views.generic import View, ListView, DetailView
from .forms import RegisterForm, LoginForm, CommentForm
from .models import User, Movie, Movie_rating, Movie_hot from .models import User, Movie, Movie_rating, Movie_hot
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
BASE = os.path.dirname(os.path.abspath(__file__)) BASE = os.path.dirname(os.path.abspath(__file__))
# 首页视图 # 首页视图
@ -76,3 +71,47 @@ class MovieDetailView(DetailView):
# 搜索电影视图
class SearchView(ListView):
model = Movie
template_name = 'movie/search.html'
paginate_by = 15
context_object_name = 'movies'
page_kwarg = 'p'
def get_queryset(self):
movies = Movie.objects.filter(name__icontains=self.request.GET.dict()['keyword'])
return movies
def get_context_data(self, *, object_list=None, **kwargs):
context = super(SearchView, self).get_context_data(*kwargs)
paginator = context.get('paginator')
page_obj = context.get('page_obj')
pagination_data = self.get_pagination_data(paginator, page_obj)
context.update(pagination_data)
context.update({'keyword': self.request.GET.dict()['keyword']})
return context
def get_pagination_data(self, paginator, page_obj, around_count=2):
current_page = page_obj.number
if current_page <= around_count + 2:
left_pages = range(1, current_page)
left_has_more = False
else:
left_pages = range(current_page - around_count, current_page)
left_has_more = True
if current_page >= paginator.num_pages - around_count - 1:
right_pages = range(current_page + 1, paginator.num_pages + 1)
right_has_more = False
else:
right_pages = range(current_page + 1, current_page + 1 + around_count)
right_has_more = True
return {
'left_pages': left_pages,
'right_pages': right_pages,
'current_page': current_page,
'left_has_more': left_has_more,
'right_has_more': right_has_more
}

@ -19,7 +19,7 @@
<div class="container"> <div class="container">
<div class="jumbotron"> <div class="jumbotron">
<h1>项目简介</h1> <h1>项目简介</h1>
<p>....................</p> <p>电影推荐系统</p>
<!-- 其他简介内容 --> <!-- 其他简介内容 -->
</div> </div>
</div> </div>

@ -0,0 +1,79 @@
{% extends 'movie/base.html' %}
{% block content %}
<style>
.item {
display: block;
float: left;
font-size: 15px;
text-align: center;
margin: 0 25px 10px 0;
width: 200px;
}
.item .poster_div {
height: 320px;
overflow: hidden;
}
li {
height: 45px;
width: 115px;
}
</style>
<!--展示电影图片用-->
<div class="container">
<div class="alert alert-success" role="alert">关于 <span
style="font-size: x-large;color: orangered">{{ keyword }}</span> 的搜索结果
</div>
{% for movie in movies %}
<a href="{% url 'movie:detail' movie.pk %}" class="item">
<div class="poster_div">
<img src="/static/movie/poster/{{ movie.imdb_id }}.jpg" alt="">
<p>{{ movie.name }}
<strong style="color:#e09015">{{ movie.get_score }}</strong>
</p>
</div>
</a>
{% endfor %}
</div>
<nav style="text-align: center;">
<ul class="pagination">
{#previous page#}
{% if page_obj.has_previous %}
<li><a href="{% url 'movie:search' %}?keyword={{ keyword }}&p={{ page_obj.previous_page_number }}">上一页</a></li>
{% else %}
<li class="disabled"><a href="javascript:void(0);">上一页</a></li>
{% endif %}
{% if left_has_more %}
<li><a href="{% url 'movie:search' %}?keyword={{ keyword }}&p=1">1</a></li>
<li><a href="javascript:void(0)">...</a></li>
{% endif %}
{# 左边的page#}
{% for left_page in left_pages %}
<li><a href="{% url 'movie:search' %}?keyword={{ keyword }}&p={{ left_page }}">{{ left_page }}</a></li>
{% endfor %}
{# 当前的page#}
<li class="active"><a href="javascript:void(0);">{{ current_page }}</a></li>
{# 右边的page#}
{% for right_page in right_pages %}
<li><a href="{% url 'movie:search' %}?keyword={{ keyword }}&p={{ right_page }}">{{ right_page }}</a></li>
{% endfor %}
{% if right_has_more %}
<li><a href="javascript:void(0)">...</a></li>
<li><a href="{% url 'movie:search' %}?keyword={{ keyword }}&p={{ paginator.num_pages }}">{{ paginator.num_pages }}</a></li>
{% endif %}
{#next page#}
{% if page_obj.has_next %}
<li><a href="{% url 'movie:search' %}?keyword={{ keyword }}&p={{ page_obj.next_page_number }}">下一页</a></li>
{% else %}
<li class="disabled"><a href="javascript:void(0);">上一页</a></li>
{% endif %}
</ul>
</nav>
{% endblock %}
Loading…
Cancel
Save