Quantcast
Channel: Active questions tagged feed - Stack Overflow
Viewing all articles
Browse latest Browse all 540

sorting feed by engagement Django

$
0
0

So, I´m currently working on a Social Media application where you can share image posts. Each post can be liked, and/or disliked by each user. At the moment, the feed is sorted by most recent post, however, I would like to sort it by engagement (number_of_likes + number_of_dislikes = engagement_count --> of all users). I´m new to python and quite unsure how to start. I had a few tries of using a class model and creating a list (engagement_count) in the def index(request): in views.py but I´m quite unable to connect the dots to something that makes sense. Please help me out with some tips how to approach this... thx.Here is the code.

views.py:

def index(request):    #user_object = User.objects.get(username=request.user.username)    #user_profile = User.objects.get(user=user_object)    user_following_list =[]    feed = []    user_following = FollowersCount.objects.filter(follower=request.user.username)    for users in user_following:        user_following_list.append(users.user)    for usernames in user_following_list:        feed_lists = Post.objects.filter(user=usernames)        feed.append(feed_lists)    feed_list = list(chain(*feed))    posts = Post.objects.all()    Post.objects.order_by('number_of_likes')    return render(request, 'index.html', {'posts': posts})def like_post(request):    username = request.user.username    post_id = request.GET.get('post_id')    post = Post.objects.get(id=post_id)#set up a filter so that a user can only like it a post once --> disable in project to give unlimited likes and dislikes    like_filter = LikePost.objects.filter(post_id=post_id, username=username).first()    if like_filter == None:        new_like = LikePost.objects.create(post_id=post_id, username=username)        new_like.save()        post.number_of_likes = post.number_of_likes+1        post.save()        return redirect('/')    else:        like_filter.delete()        post.number_of_likes = post.number_of_likes - 1        post.save()        return redirect('/')def dislike_post(request):    username = request.user.username    post_id = request.GET.get('post_id')    post = Post.objects.get(id=post_id)#set up a filter so that a user can only like it a post once --> disable in project to give unlimited likes and dislikes    dislike_filter = DislikePost.objects.filter(post_id=post_id, username=username).first()    if dislike_filter == None:        new_dislike = DislikePost.objects.create(post_id=post_id, username=username)        new_dislike.save()        post.number_of_dislikes = post.number_of_dislikes+1        post.save()        return redirect('/')    else:        dislike_filter.delete()        post.number_of_dislikes = post.number_of_dislikes - 1        post.save()        return redirect('/')def upload(request):    if request.method == 'POST':        user = request.user.username        image = request.FILES.get('image_upload')        caption = request.POST['caption']        new_post = Post.objects.create(user=user, image=image, caption=caption)        new_post.save()        return redirect('/')

urls.py

urlpatterns = [    path('', views.index, name='index'),    path('signup', views.signup, name='signup'),    path('upload', views.upload, name='upload'),    path('follow', views.follow, name='follow'),    path('profile/<str:pk>', views.profile, name='profile'),    path('like-post', views.like_post, name='like-post'),    path('dislike-post', views.dislike_post, name='dislike-post'),    path('settings', views.settings, name='settings'),    path('signin', views.signin, name='signin'),    path('logout', views.logout, name='logout')]

models.py

class Post(models.Model):    id = models.UUIDField(primary_key=True, default=uuid.uuid4)    user = models.CharField(max_length=100)    image = models.ImageField(upload_to='post_images')    caption = models.TextField (max_length=100)    created_at = models.DateTimeField(default=datetime.now)    number_of_likes = models.IntegerField(default=0)    number_of_dislikes = models.IntegerField(default=0)    def __str__(self):        return self.userclass LikePost(models.Model):    post_id = models.CharField(max_length=500)    username = models.CharField(max_length=100)    def __str__(self):        return self.usernameclass DislikePost(models.Model):    post_id = models.CharField(max_length=500)    username = models.CharField(max_length=100)    def __str__(self):        return self.username

index.html

{% for post in posts reversed %}<div class="bg-white shadow rounded-md  -mx-2 lg:mx-0"><!-- post header--><div class="flex justify-between items-center px-4 py-3"><div class="flex flex-1 items-center space-x-4"><a href="#"><div class="bg-gradient-to-tr from-yellow-600 to-pink-600 p-0.5 rounded-full">  <img src="{% static 'assets/images/avatars/user.png' %}" class="bg-gray-200 border border-white rounded-full w-8 h-8"></div></a><span class="block capitalize font-semibold "><a href="/profile/{{ post.id }}">@{{ post.id }}  </a></span></div><div><a href="#"> </a><div class="bg-white w-56 shadow-md mx-auto p-2 mt-12 rounded-md text-gray-500 hidden text-base border border-gray-100  " </li></ul></div></div></div><div uk-lightbox><a href="{{post.image.url}}"><img src="{{post.image.url}}" alt=""></a></div><div class="py-3 px-4 space-y-3"> <div class="flex space-x-4 lg:font-bold"><a href="/like-post?post_id={{ post.id }}" class="flex items-center space-x-2"><div class="p-2 rounded-full text-black"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" width="25" height="25" class=""><path d="M2 10.5a1.5 1.5 0 113 0v6a1.5 1.5 0 01-3 0v-6zM6 10.333v5.43a2 2 0 001.106 1.79l.05.025A4 4 0 008.943 18h5.416a2 2 0 001.962-1.608l1.2-6A2 2 0 0015.56 8H12V4a2 2 0 00-2-2 1 1 0 00-1 1v.667a4 4 0 01-.8 2.4L6.8 7.933a4 4 0 00-.8 2.4z" /></svg>                                            {% if post.number_of_likes == 0 %}<p>no likes</p>                                            {% else%}<p>liked by {{ post.number_of_likes }} user</p>                                            {% endif %}</div></a><a href="/dislike-post?post_id={{ post.id }}" class="flex items-center space-x-2"><div class="p-2 rounded-full text-black"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" width="25" height="25" class=""><path d="M2 10.5a1.5 1.5 0 113 0v6a1.5 1.5 0 01-3 0v-6zM6 10.333v5.43a2 2 0 001.106 1.79l.05.025A4 4 0 008.943 18h5.416a2 2 0 001.962-1.608l1.2-6A2 2 0 0015.56 8H12V4a2 2 0 00-2-2 1 1 0 00-1 1v.667a4 4 0 01-.8 2.4L6.8 7.933a4 4 0 00-.8 2.4z" /></svg>                                            {% if post.number_of_dislikes == 0 %}<p>no dislikes</p>                                            {% else%}<p>disliked by {{ post.number_of_dislikes }} user</p>                                            {% endif %}</div></a></a><a href="{{ post.image.url }}" class="flex items-center space-x-2 flex-1 justify-end" download><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="25" height="25" preserveAspectRatio="xMidYMid meet" viewBox="0 0 16 16"><g fill="currentColor"><path d="M8.5 1.5A1.5 1.5 0 0 1 10 0h4a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V2a2 2 0 0 1 2-2h6c-.314.418-.5.937-.5 1.5v6h-2a.5.5 0 0 0-.354.854l2.5 2.5a.5.5 0 0 0 .708 0l2.5-2.5A.5.5 0 0 0 10.5 7.5h-2v-6z"/></g></svg></a></div></div></div>                        {% endfor %}

Viewing all articles
Browse latest Browse all 540

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>