diff --git a/MoviesRecommend/movie/__pycache__/forms.cpython-38.pyc b/MoviesRecommend/movie/__pycache__/forms.cpython-38.pyc index b9c6ed8..19dc131 100644 Binary files a/MoviesRecommend/movie/__pycache__/forms.cpython-38.pyc and b/MoviesRecommend/movie/__pycache__/forms.cpython-38.pyc differ diff --git a/MoviesRecommend/movie/__pycache__/models.cpython-38.pyc b/MoviesRecommend/movie/__pycache__/models.cpython-38.pyc index c92c1ae..7ad0b13 100644 Binary files a/MoviesRecommend/movie/__pycache__/models.cpython-38.pyc and b/MoviesRecommend/movie/__pycache__/models.cpython-38.pyc differ diff --git a/MoviesRecommend/movie/__pycache__/urls.cpython-38.pyc b/MoviesRecommend/movie/__pycache__/urls.cpython-38.pyc index 1ffc858..5b98ede 100644 Binary files a/MoviesRecommend/movie/__pycache__/urls.cpython-38.pyc and b/MoviesRecommend/movie/__pycache__/urls.cpython-38.pyc differ diff --git a/MoviesRecommend/movie/__pycache__/views.cpython-38.pyc b/MoviesRecommend/movie/__pycache__/views.cpython-38.pyc index 26f7bbf..ca7a7f0 100644 Binary files a/MoviesRecommend/movie/__pycache__/views.cpython-38.pyc and b/MoviesRecommend/movie/__pycache__/views.cpython-38.pyc differ diff --git a/MoviesRecommend/movie/urls.py b/MoviesRecommend/movie/urls.py index 08c4bf3..5126bc6 100644 --- a/MoviesRecommend/movie/urls.py +++ b/MoviesRecommend/movie/urls.py @@ -13,22 +13,10 @@ urlpatterns = [ # 电影详情页面 path('detail/', 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/', views.RatingHistoryView.as_view(), name='history'), -# 删除记录 -path('del_rec/', views.delete_recode, name='delete_record'), -# 推荐页面 -path('recommend', views.RecommendMovieView.as_view(), name='recommend')''' \ No newline at end of file + diff --git a/MoviesRecommend/movie/views.py b/MoviesRecommend/movie/views.py index b76d73d..26def37 100644 --- a/MoviesRecommend/movie/views.py +++ b/MoviesRecommend/movie/views.py @@ -1,11 +1,6 @@ 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 .forms import RegisterForm, LoginForm, CommentForm 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__)) # 首页视图 @@ -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 + } \ No newline at end of file diff --git a/MoviesRecommend/templates/movie/index.html b/MoviesRecommend/templates/movie/index.html index 7667a3f..61f91ab 100644 --- a/MoviesRecommend/templates/movie/index.html +++ b/MoviesRecommend/templates/movie/index.html @@ -19,7 +19,7 @@

项目简介

-

....................

+

电影推荐系统

diff --git a/MoviesRecommend/templates/movie/search.html b/MoviesRecommend/templates/movie/search.html new file mode 100644 index 0000000..fcfb648 --- /dev/null +++ b/MoviesRecommend/templates/movie/search.html @@ -0,0 +1,79 @@ +{% extends 'movie/base.html' %} +{% block content %} + + +
+ + + {% for movie in movies %} + +
+ +

{{ movie.name }} + {{ movie.get_score }} +

+
+
+ {% endfor %} +
+ + +{% endblock %} \ No newline at end of file