์์ฑ๋ ๊ฒ์๋ฌผ์ ์ ๋ชฉ, ํ์ , ์์ฑ์๊ฐ
์ ํ์ธํ ์ ์์์ผ๋ ํด๋น ๋ด์ฉ์ ํ์ธํ๊ณ ์์
ํ๋ ๋ถ๋ถ์ด ํ์ํ๋ค๋ ์๊ฒฌ์ด ๋์ ์์
๊ฒ์๋ฌผ์ ์์ฑํ ๋ ์ฌ์ฉํ๋ ์ฃผํ๋์ด ๋ง๋ ํ์ด์ง์ ๋์ผํ UI๋ฅผ ์ฌ์ฉ
ํด ์ฌ์ฉ์๊ฐ ์์ ์ ์น์ํ๊ฒ ํ ์ ์๋๋ก ์์ฑ
์ฌ์ฉ์๊ฐ ์ด๋ฏธ ์์ฑํ ๋ด์ฉ์ ๊ธฐ๋ณธ ๋ด์ฉ์ผ๋ก ๊ฐ์ง๊ณ ์์ด์ผ ํ๋ฉฐ, ์ฌ์ฉ์๊ฐ ์ด๋ค ๊ฐ์ผ๋ก ๋ฐ๊พธ๋์ง ํ์ธํ ์ ์๋๋ก ๋ฐ๋๋ ํ์ ๊ฐ
์ ํ์
์ทจ์
ํค๋ฅผ ๋๋ฅด๋ฉด ๊ฒ์๋ฌผ ๋ฆฌ์คํธ๊ฐ ์๋ mypage
๋ก ๋ค์ ๋์๊ฐ ์ ์๋๋ก location.href
๋ฅผ ์ฌ์ฉ
form
ํ๊ทธ๋ฅผ ์ธ ์ ๋ฐ์ ์๊ณ , form
ํ๊ทธ์ ๋ด์ฉ์ ๋ฐฑ์ค๋๋ก ๋ณด๋ด๋ ๋ฒํผ์ ๋ฐ๋ก ์์๊ธฐ ๋๋ฌธ์ ํด๋น ๊ฒฐ์ ๋ด๋ฆผ์์ฑ์๊ฐ
์๋ ๋ณํ๊ฐ ์์ผ๋ฉด ์ข์ ๊ฒ ๊ฐ์ datetime
๋ชจ๋์ datetime.datetime.now()
๋ฅผ ์ฌ์ฉํด ํ์ฌ ๋ณ๊ฒฝ ์๊ฐ์ ์ ์ฅํด ์ค
์ฝ๋
post/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('detail/edit/<int:id>/', views.edit, name='edit'),
path('detail/update/<int:id>', views.update, name='update'),
]
post/views.py
from django.shortcuts import render, redirect
from .models import PostModel
from movie.models import Movie
from datetime import datetime
# ๊ฒ์๋ฌผ ํ์ธ
def edit(request, id):
article = PostModel.objects.get(id=id)
movie = Movie.objects.get(id=article.title_id)
context = {
'article': article,
'movie': movie,
}
return render(request, 'post/edit.html', context)
# ๊ฒ์๋ฌผ ์
๋ฐ์ดํธ
def update(request, id):
if request.method == 'POST':
user = request.user
article = PostModel.objects.get(id=id)
article.score = request.POST.get('myRange')
article.content = request.POST.get('comment')
article.created_at = datetime.now()
article.save()
all_post = PostModel.objects.filter(author_id=user.id).order_by('-created_at')
return render(request, 'post/mypage.html', {'username': user, 'posts': all_post})
templates/post/edit.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Edit</title>
</head>
<body>
<div class="modal" id="modal">
<div class="modal_back">
<div class="modal_content">
<div class="modal_title">
๊ฒ์๊ธ ์์
</div>
<form action="/detail/update/{{ article.id }}" method="POST">
{% csrf_token %}
<div class="modal_component">
<div class="title">
{{ movie.title }}
</div>
<div>
<div class="slidecontainer" >
<span>ํ์ ์ ๋ฐ๊ฟ์ฃผ์ธ์.</span><br>
<input required type="range" min="1" max="5" step="0.1" value="{{ article.score }}" class="slider" id="myRange" name="myRange" oninput="document.getElementById('score').innerHTML=this.value;" ><br>
<span id="score" class="score">{{ article.score }}</span>
</div>
</div>
<div>
<textarea required name="comment" id="comment" class="comment" placeholder="">{{ article.content }}</textarea>
</div>
<div>
<button type="button" onclick="location.href='/mypage'" class="btn1" id="btn1">์ทจ์</button> <button class="btn2" type="submit">์์ฑ</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
recommend/views.py
from django.shortcuts import render, redirect
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
from movie.models import Movie, Taste
movies = pd.read_csv('recommend/movies.csv')
ratings = pd.read_csv('recommend/ratings.csv')
### item_based_filtering ####
movie_ratings = pd.merge(ratings, movies, on='movieId')
user_title = movie_ratings.pivot_table(
'rating', index='title', columns='userId')
user_title = user_title.fillna(0)
item_based_collab = cosine_similarity(user_title, user_title)
item_based_collab = pd.DataFrame(
item_based_collab, index=user_title.index, columns=user_title.index)
def item_based_filtering(movie):
# ์ฃผ์ด์ง ์ํ ์ค cosine-similarity ๊ฐ์ด ๊ฐ์ฅ ํฐ ์์ผ๋ก 20๋ฒ์งธ๊น์ง ๋ณ์์ ์ ์ฅ
movie_list = item_based_collab[movie].sort_values(ascending=False)[1:21]
print(f'movie_list : {movie_list.index}')
return movie_list.index
def taste(request):
if request.method == 'POST':
user = request.user
print(f'user:{user}')
print(user.id)
for key, value in request.POST.items():
if key == "csrfmiddlewaretoken":
continue
else:
Taste.objects.create(
user=user,
# movie=Movie.objects.get(title=value),
movie_id=value,
)
movies = []
movie = Movie.objects.get(id=value)
print(movie.title)
a = item_based_filtering(movie.title)
for i in a:
movie = Movie.objects.get(title=i)
movie.tags = ", ".join(
list(movie.tag.all().values_list('tag', flat=True)))
movies.append(movie)
print(movies, type(movies))
return render(request, 'movie/home.html', {'movies': movies})
templates/recommend/taste.html
{% extends 'base.html' %}
{% block content %}
<form action="{% url 'recommend:taste' %}" method="post">
{% csrf_token %}
{% if movies %}
{% for movie in movies %}
<div>
<input type="checkbox" name="title" value="{{ movie.id }}">
<label for="title">{{ movie.title }}</label>
</div>
{% endfor %}
{% endif %}
<button type="submit">OK</button>
</form>
{% endblock %}