wzy 6 months ago
parent a0a2a7e643
commit 3227e5af28

@ -6,14 +6,14 @@ app_name='movie'
urlpattterns=[ urlpattterns=[
path('',views.indexView.as_view(),name='index'), path('',views.IndexView.as_view(),name='index'),
path('hot',views.PopularMovieView.as_view(),name='hot'), 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('logout',views.UserLogout.as_view(),name='logout'),
path('register',views.RegisterView.as_view(),name='register'), path('register',views.RegisterView.as_view(),name='register'),
path('tag',views.TagView.as_view(),name='tag'), path('tag',views.TagView.as_view(),name='tag'),
path('search',views.SearchView.as_view(),name='search'), path('search',views.SearchView.as_view(),name='search'),
path('detail/<int:pk>',views.MovieDetailView.as_view(),name='detail'), path('detail/<int:pk>',views.MovieDetailView.as_view(),name='detail'),
path('history/<int:pk>',views.RatingHistoryView.as_view(),name='history'), path('history/<int:pk>',views.RatingHistoryView.as_view(),name='history'),
path('del_rec/<int:pk>',views.delete_recode,name='delete_recode') path('del_rec/<int:pk>',views.delete_recode,name='delete_recode'),
] ]

@ -128,7 +128,7 @@ class TagView(ListView):
return movies[100:200] return movies[100:200]
else: 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) print(movies)
return movies[:100] return movies[:100]
def get_context_data(self,*,object_list=None,**kwargs): def get_context_data(self,*,object_list=None,**kwargs):
@ -210,119 +210,148 @@ class SearchView(ListView):
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'))
Loading…
Cancel
Save