From 3227e5af2805b83824de72f7b66d55f00548be81 Mon Sep 17 00:00:00 2001 From: wzy <1458201660@qq.com> Date: Fri, 31 May 2024 12:18:40 +0800 Subject: [PATCH] 0531 --- .../movie/__pycache__/forms.cpython-312.pyc | Bin 3820 -> 3820 bytes .../movie/__pycache__/model.cpython-312.pyc | Bin 7219 -> 7219 bytes .../movie/__pycache__/urls.cpython-312.pyc | Bin 1490 -> 1490 bytes .../movie/__pycache__/views.cpython-312.pyc | Bin 4732 -> 16863 bytes djangoProject2/movie/urls.py | 6 +- djangoProject2/movie/views.py | 255 ++++++++++-------- 6 files changed, 145 insertions(+), 116 deletions(-) diff --git a/djangoProject2/movie/__pycache__/forms.cpython-312.pyc b/djangoProject2/movie/__pycache__/forms.cpython-312.pyc index 6ed2a4624437d29771ce8a3639da045bc9f695a9..c30a24f782fa9c4e46479579d2c330051c010087 100644 GIT binary patch delta 18 YcmaDO`$m@YG%qg~0}wQ9xa~I2~mPnN=BcHa}$k$Or(0OQZ-97`03RYLk|G81p%+EPvJ@#6AC@Ijj-|vA%oKsR5Cs|_Qjf3R7)fH$0pXDZp&uc{`Sc-$Z9 zId`$UAOt<^q@H#rcLoP{?`!Y#obR4<_Fo+iD+S@>Eyqqg)lO0Wjt@#O<)`Tr&BWZ@XJ>dvB z5@n&XgfrxXG$U(@yAtk@JK+g=65f!Pq?zNsM0uz@Q4y+0_(T3gAQYgfC_S=G>6-RG zvO|k2vAdMqspM<_(VEv_Y#5aP&^jH}N3oWR6l;x^y{4CQL$xH;2C4Rf)H;&tfYh>r z)OwQYgj82S>UxsuhEz|~p|;yVQoWGsD@bi*%P&%)CX!bHdH$$Vt+9cm1|YSvAhnsT zf*LL7=wS6N?D9a6mduGL&qs!%d{8Ip`y*#1!~U`H(PU6B8M){v8|5T37iD8ybSNpA zxajF9$44QP`D!eBMlv6Y@ktW1c1M$uSRCWGaI&C?{fX!>WJkH)F)krl4vh`RMoHAR ze=L!RjwbOT$VkjrczBDUf9!NDD%r_jn2R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YiEP-O0tVMD_sz?n)sDlU_5CO8R zm=#D&&J?sV;|4Ms7;cElPLH1$-^o*?2vTAXBs6)896)SG5a9$Ogh7Neh;W%)C#0tD z0b(kE2u%>74I(^2gcpz~2D!|kNFK!Z0TYui3t90qvZ_L?0EuZTOnz>^kkNT^r-Lgi jNMPmUe-2_?g+LZacX9G$ImZ}&IY!k96<-;E6j&z!#}`X{ diff --git a/djangoProject2/movie/urls.py b/djangoProject2/movie/urls.py index 35f6ed4e..a8b0279b 100644 --- a/djangoProject2/movie/urls.py +++ b/djangoProject2/movie/urls.py @@ -6,14 +6,14 @@ app_name='movie' urlpattterns=[ - path('',views.indexView.as_view(),name='index'), + path('',views.IndexView.as_view(),name='index'), path('hot',views.PopularMovieView.as_view(),name='hot'), - path('login',views.loginView.as_view(),name='login'), + path('login',views.LoginView.as_view(),name='login'), path('logout',views.UserLogout.as_view(),name='logout'), path('register',views.RegisterView.as_view(),name='register'), path('tag',views.TagView.as_view(),name='tag'), path('search',views.SearchView.as_view(),name='search'), path('detail/',views.MovieDetailView.as_view(),name='detail'), path('history/',views.RatingHistoryView.as_view(),name='history'), - path('del_rec/',views.delete_recode,name='delete_recode') + path('del_rec/',views.delete_recode,name='delete_recode'), ] \ No newline at end of file diff --git a/djangoProject2/movie/views.py b/djangoProject2/movie/views.py index 9a911759..cf0313e0 100644 --- a/djangoProject2/movie/views.py +++ b/djangoProject2/movie/views.py @@ -128,7 +128,7 @@ class TagView(ListView): return movies[100:200] else: - movies=movie.objects.filter(genre__name=self.request.GET.dict()['genre']) + movies=Movie.objects.filter(genre__name=self.request.GET.dict()['genre']) print(movies) return movies[:100] def get_context_data(self,*,object_list=None,**kwargs): @@ -206,123 +206,152 @@ class SearchView(ListView): 'current_page':current_page, 'left_has__more':left_has_more, 'right_has__more':right_has_more - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + } +class RegisterView(View): + def get(self,request): + return render(request,'movie/register.html') + def post(self,request): + form=RegisterForm(request.POST) + if form.is_valid(): + form.save() + return redirect(reverse('movie.index')) + else: + errors=form.get_errors() + for error in errors: + messages.info(request,error) + print(form.errors.get_json_data()) + return redirect(reverse('movie:register')) + +class LoginView(View): + def get(self,request): + return render(request,'movie/login.html') + + def post(self,request): + form=LoginForm(request.POST) + if form.is_valid(): + name=form.cleaned_data.get('name') + pwd=form.cleaned_data.get('password') + remember=form.cleaned_data.get('remember') + user=User.objects.filter(name=name,password=pwd).first() + if user: + if remember: + request.session.set_expiry(None) + else: + request.session.set_expiry(0) + request.session['user_id']=user.id + return redirect(reverse('movie.index')) + else: + messages.info(request,'用户名或密码错误') + return redirect(reverse('movie:login')) + else: + errors=form.get_errors() + for error in errors: + messages.info(request,error) + return redirect(reverse('movie:login')) +class UserLogout(request): + request.session.set_expiry(-1) + return redirect(reverse('movie:index')) + +class MovieDetailView(DetailView): + model=Movie + template_name='movie/detail.html' + context_object_name = 'movie' + def get_context_data(self,**kwargs): + context=super().get_context_data(**kwargs) + login=True + try: + user_id=self.request.session['user_id'] + except KeyError as e: + login=False + + pk=self.kwargs['pk'] + movie=Movie.object.get(pk=pk) + if login: + user=User.objects.get(pk=user_id) + rating=Movie_rating.objects.filter(user=user,movie=movie).first() + score=0 + comment='' + if rating: + score=rating.score + comment=rating.comment + context.update({'score':score,'comment':comment}) + similary_movies=movie.get_similarity() + context.update({'similary_movies':similary_movies}) + context.update({'login':login}) + return context + def post(self,request,pk): + form=CommentForm(request.POST) + if form.is_valid(): + score=form.cleaned_data.get('score') + comment=form.cleaned_data.get('comment') + user_id=request.session['user_id'] + user=User.objects.get(pk=user_id) + movie=Movie.object.get(pk=pk) + rating=Movie_rating.object.filter(user=user,movie=movie).first() + if rating: + rating.score=score + rating.comment=comment + rating.save() + else: + rating=Movie_rating(user=user,movie=movie,score=score,comment=comment) + rating.save() + messages.info(request,"评论成功!") + else: + messages.info(request,"评论不能为空!") + return redirect(reverse('movie:detail',args=(pk,))) + +class RatingHistoryView(DetailView): + model=User + template_name='movie/history.html' + qcontext_object_name='user' + + def get_context_data(self,**kwargs): + context=super().get_context_data(**kwargs) + user_id=self.request.session['user_id'] + user=User.objects.get(pk=user_id) + ratings=Movie_rating.objects.filter(user=user).order_by('-score') + context.update({'ratings':ratings}) + return context +def delete_recode(request,pk): + movie=Movie.objects.get(pk=pk) + user_id=request.session['user_id'] + user=User.objects.get(pk=user_id) + rating=Movie_rating.objects.get(user=user,movie=movie) + rating.delete() + messages.info(request,f'删除{movie.name}评分记录成功!') + return redirect(reverse('movie:history',args=(user_id,))) +class recommendMovieView(ListView): + model=Movie + template_name='movie/recommend.html' + paginate_by=15 + context_object_name='movies' + ordering='movie_rating__score' + page_kwarg='p' + def __int__(self): + super().__init__() + self.K=20 + self.N=10 + self.cur_user_movie_qs=None + + def get_user_sim(self): + user_sim_dct=dict() + cur_user_id=self.request.session['user_id'] + cur_user=User.objects.get(pk=cur_user_id) + other_users=User.objects.exclude(pk=cur_user_id) + self.cur_user_movie_qs=Movie.objects.filter(user=cur_user) + for user in other_users: + user_sim_dct[user.id]=len(Movie.object,filter(user=user)&self.cur_user_movie_qs) + print("user similarity calculated!") + return sorted(user_sim_dct.items(),key=lambda x:-x[1][:self.K]) + def get_recommend_movie(self,user_lst): + movie_val_dct=dict() + for user,_in user_lst: + movie_set=Movie.objects.filter(user=user).exclude(id__in=self.cur_user_movie_qs).annotate(score=F('movie_rating__score')*F('movie_rating__score')) \ No newline at end of file