如果用户喜欢使用Django的帖子,则显示“赞”和“不喜欢”按钮

时间:2020-07-14 18:41:54

标签: python django model cs50

感谢您抽出宝贵的时间阅读这篇文章。

我正在为CS50W设计一个项目,其中必须显示一系列用户可以喜欢然后不喜欢的帖子。

我可以成功显示喜欢的次数,但是一旦完成,我就无法将“赞”按钮更改为“不喜欢”。

以下是相关代码:

views.py

function getDistTime(resources){
    const promises = [];

    for (var i = 0; i < resources.data.length; i++) {
        var origin1 = $("#citystate").val();
        var destinationA = resources.data[i]['DEMOBILIZATION CITY'] + ',' + resources.data[i]['DEMOBILIZATION STATE'];
        promises.push(googleRequest(origin1, destinationA));
    }

    return Promise.all(promises).then((results) => {
        return results.map((result, i) => {
            return {
                ...resources.data[i],
                DISTANCE_MI: result[0],
                ACTUAL_DUR_HR: result[1],
                FINANCE_DUR_HR: (result[0] / 45.0).toFixed(2)
            };
        });
    });
}

getDistTime(resources).then(result => {
    //result is now "final_rows"
});

然后创建models.py

def index(request):
    return render(request, "network/index.html", {
        "posts": Post.objects.all().order_by('time'),
        "form": NewPost(),
        "likes": Like.objects.all(),
    })

...

def like(request, post_id):
    if request.method == "POST":
        #make sure user can't like the post more than once. 
        user = User.objects.get(username=request.user.username)
        #find whatever post is associated with like
        post = Post.objects.get(id=post_id)
        #access liked values: 

        if Like.objects.filter(user=user, post=post).exists():
            Like.alreadyLiked = True
            return HttpResponseRedirect(reverse('index'))
        else: 
            newLike = Like(user=user, post=post)
            newLike.alreadyLiked = True
            post.likes += 1
            post.save()
            newLike.save()
            return HttpResponseRedirect(reverse('index'))
    

最后,显示按钮的index.html。

from django.contrib.auth.models import AbstractUser
from django.db import models
import datetime


class User(AbstractUser):
    pass

class Post(models.Model):
    text = models.CharField(max_length=127)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="author")
    time = models.TimeField(auto_now=False, auto_now_add=True)
    likes = models.PositiveIntegerField(default=0)

    def __str__(self):
        if self.likes == 1:
            return f"{self.user} ({self.time}): {self.text} - {self.likes} Like"
        elif self.likes == 0:
            return f"{self.user} ({self.time}): {self.text} - No Likes"
        else:
            return f"{self.user} ({self.time}): {self.text} - {self.likes} Likes"

class Like(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="users")
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name="posts")
    alreadyLiked = models.BooleanField(default=False)

    def __str__(self):
        return f"{self.user} liked {self.post}"

我的想法是,在喜欢和讨厌的表单部分中,Django模板应确定登录用户是否已喜欢该帖子,在这种情况下,它将显示一个“不喜欢”按钮而不是“喜欢”按钮。最重要的是,此段:

{% extends "network/layout.html" %}

{% block body %}



<form action="/newPost" method="post" name="newPost">
{% csrf_token %}
{{ form.content }}
<input type="submit">
</form>


{% for post in posts %}


<div id="post{{post.id}}">{{ post }}

{% if likes.post == post and likes.user == user.username %}

    <form action="{% url 'dislike' post.id  %}" method="post" name="dislike" id="dislikeform">
        {% csrf_token %}
        <button type='submit' name='dislike' value="{{ post.id }}" class="btn btn-primary">Dislike</button>
    </form>

{% else %}    

    <form action="{% url 'like' post.id  %}" method="post" name="like" id="likeform">
        {% csrf_token %}
        <button type='submit' name='like' value="{{ post.id }}" class="btn btn-primary">Like</button>
    </form>

{% endif %}

{% if follows.following == True %}

    <form action="{% url 'unfollow' post.user  %}" method="post" name="follow">
        {% csrf_token %}
        <button type='submit' name='follow' value="{{ post.user }}" class="btn btn-primary">Follow</button>
    </form>

{% else %}

    <form action="{% url 'follow' post.user  %}" method="post" name="follow">
        {% csrf_token %}
        <button type='submit' name='follow' value="{{ post.user }}" class="btn btn-primary">Follow</button>
    </form>

{% endif %}

</div>

{% endfor %}

{% endblock %}

“关注”和“取消关注”按钮也有类似的问题,但是我认为,如果我可以实现第一个问题的解决方案,也可以应用它。

1 个答案:

答案 0 :(得分:0)

在重新考虑了模型之间的关系后将其解决。

我在帖子模型中添加了一个新字段:

class Post(models.Model):
    text = models.CharField(max_length=127)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="author")
    time = models.TimeField(auto_now=False, auto_now_add=True)
    likes = models.PositiveIntegerField(default=0)
    user_likes = models.ManyToManyField(User)

用户现在与一个ManytoManyField帖子相关,这使我可以在这些模型之间创建关系。

然后在views.py中输入

def like(request, post_id):
    if request.method == "POST":
        #make sure user can't like the post more than once. 
        user = User.objects.get(username=request.user.username)
        #find whatever post is associated with like
        post = Post.objects.get(id=post_id)

        newLike = Like(user=user, post=post)
        newLike.alreadyLiked = True

        post.likes += 1
        #adds user to Post 
        post.user_likes.add(user)
        post.save()
        newLike.save()
        return HttpResponseRedirect(reverse('index'))

我为不喜欢做过同样的事情,并将为追随和取消关注尝试同样的事情。

最后,在index.html中:

{% if user in post.user_likes.all %}

    <form action="{% url 'dislike' post.id  %}" method="post" name="dislike" id="dislikeform">
        {% csrf_token %}
        <button type='submit' name='dislike' value="{{ post.id }}" class="btn btn-primary">Dislike</button>
    </form>

{% else %}    

    <form action="{% url 'like' post.id  %}" method="post" name="like" id="likeform">
        {% csrf_token %}
        <button type='submit' name='like' value="{{ post.id }}" class="btn btn-primary">Like</button>
    </form>

{% endif %}

现在一切正常。