推荐系统的Models.py

时间:2019-02-08 13:52:44

标签: django database django-models

我的Web应用程序以自举卡格式显示从JSON文件中拍摄的电影列表。每个电影都存在一个单独的卡。我需要对用户的n部电影进行用户评分(1-5),如果用户不对其进行评分,则默认值为0。

我想以行列格式将其保存在数据库中,其中每一行代表一个用户,列代表电影,表格代表每个用户对每部电影的评分。

我的views.py看起来像这样:

from django.shortcuts import render
import requests
import json
from .models import *
from . import forms
def list(request):
        movies_data=open('movies.json').read()
        movies = json.loads(movies_data)
        return render(request, 'home.html', {'movies' : movies})

我的home.html看起来像这样:

    {% extends 'base.html' %}

{% block content %}
    <div class="jumbotron jumbotron-fluid">
    <div class="container">
    <center> <h1 class="display-3">Welcome to Movie Finder!</h1>
    <p class="lead"> Your one-stop and best recommender of movies.</p></center>


            <div class ="container">
            <div class="row">
            {% for movie in movies %}
            <div class="col-sm-3 col-lg-3">
            <div class="card h-8">
            <div class="card-group">

                <div class="card text-center" style="width:20rem;">
                <img class="card-img-top" src="{{movie.img_url}}" alt="Card image cap">
                <div class="card-body">
                <h5 class="card-title">{{movie.title}}</h5>
                <h5 class="card-title">Rating : {{movie.users_rating}}</h5>
                <h5 class="card-title">Year : {{movie.year}}</h5>
                <p class="card-text"><h5>Description:</h5> {{movie.description|truncatewords:10}} </p>
                <br></br
                <a href="{{movie.imdb_url}}" class="btn btn-dark">Explore</a>
            </div>
        </div>
            </div>
        </div>
    </div>

    {% endfor %}
</div>
</div>

{% endblock %}

我的models.py和输入内容的程序应该是什么?我在Google上搜索了很多,但无法达到目的。谁能帮忙吗?

1 个答案:

答案 0 :(得分:0)

我还没有完全理解您的问题,但是对于支持您的需求的模型: 请注意,我使用Client而不是User,因为User是django的默认模型,您也可以使用它,它对处理登录和会话非常有用 Check it here

from django.db import models

class Client(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    ...

class Movie(models.Model):
    movie_name = models.CharField(max_length=30)
    ...

class Rating(models.Model):
    user = models.ForeignKey(Client, on_delete=models.CASCADE )
    movie = models.ForeignKey(Movie, on_delete=models.CASCADE )
    rate = models.IntegerField(default=0)

拥有模型后,您需要将表单添加到home.html ..这意味着

  • 为您的url.py添加新路径
  • 创建一个函数来接收数据并将其保存在views.py
  • 在您的home.html
  • 中添加表单输入

我正在发布一个简单的代码,但是您必须安排它以处理异常并按需完成。

url.py

(...),
# must have the <int:user> to receive the pk of the user
path('^home/<int:user>/rating/$', views.rating, namespace='rating'),

views.py

def rating(request, user):
    base_context = {'request': request}
    if (request.method == 'POST'):
        # access to the POST data, as you want N movies, you could name them movieX with X been a number, you should do a loop here to iterate. Im making a one case only
        movie = int(request.POST['movie1'])
        rating = request.POST['rating1']
        # Check if rating exists
        try:
            r = Rating.objects.get(user.pk=user, movie.pk=movie)
        except:
            r = Rating()
        r.rating = int(rating)
        r.save()
    return redirect('appname:home')

home.html

这是您直接输入的简单表格look here