2017-10-17 21 views
0

因此,我正在使这个通知系统应该显示/告诉用户何时他人已经喜欢或评论他们的帖子。但是,不要告诉用户何时有人喜欢你的帖子,而是记录你的个人活动,并显示你自己所做的所有喜欢和评论。我知道为什么会发生这种情况(毕竟我设置了它),我只是不知道如何反转它,以便发布作者得到通知而不是当前用户。获取/显示来自另一个应用程序的帖子作者的通知系统

我想我有我需要的所有信息,我只是不知道如何进入其他应用程序模型,获取额外的信息我没有,然后显示通知/作者。请帮忙。

此外,请忽略马虎进口,我知道需要清理。

通知应用程序的观点:

from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger 
from django.shortcuts import render,get_object_or_404,redirect 
from django.utils import timezone 
from feed.models import UserPost,UserComment 
from users.models import UserProfile 
from notify.models import UserNotification 
from feed.forms import PostForm,CommentForm 
from django.urls import reverse_lazy 
from django.contrib.auth.decorators import login_required 

from django.contrib.auth import get_user_model 

from django.contrib.auth.mixins import LoginRequiredMixin 
from braces.views import SelectRelatedMixin 

from django.views.generic import (TemplateView,ListView, 
            DetailView,CreateView, 
            UpdateView,DeleteView, 
            RedirectView,) 

User = get_user_model() 

class UserNotifications(LoginRequiredMixin,ListView): 
    login_url = 'account_login' 
    model = UserNotification 
    template_name = 'notify/usernotification_list.html' 
    context_object_name = 'notifies' 
# paginate_by = 25 

    def get_queryset(self): 
     return UserNotification.objects.filter(user=self.request.user) 

class NotifyMarkRead(RedirectView): 
    def get_redirect_url(self,pk): 
     obj = get_object_or_404(UserNotification,pk=pk) 
     if obj.read == False: 
      obj.read = True 
      obj.save() 
     else: 
      obj.read = False 
      obj.save() 
     return obj.get_absolute_url() 

通知应用模型:

from django.db import models 
from django.contrib.auth import get_user_model 
from django.db.models.signals import post_save 
from django.dispatch import receiver 
from django.core.urlresolvers import reverse 

User = get_user_model() 

# Create your models here. 
class UserNotification(models.Model): 
    user = models.ForeignKey(User,related_name='user',null=True) 
    post = models.ForeignKey('feed.UserPost',related_name='post') 
    timestamp = models.DateTimeField(auto_now_add=True) 
    notify_type = models.CharField(max_length=6) 
    read = models.BooleanField(default=False) 

    def get_absolute_url(self): 
     return reverse('notify:user_notifications') 

    def __str__(self): 
     return str(self.user) 

想必,我已经将它设置的方式,我需要去和获取基础上,笔者帖子记录在UserNotification模型中?

饲料应用车型:

from django.db import models 
from django.core.urlresolvers import reverse 
from django.conf import settings 

from django.contrib.auth import get_user_model 
User = get_user_model() 

# Create your models here. 
class UserPost(models.Model): 
    author = models.ForeignKey(User,related_name='userpost',null=True) 
    post_date = models.DateTimeField(auto_now_add=True) 
    title = models.CharField(max_length=150,blank=False) 
    post_body = models.TextField(max_length=1000,blank=False) 
    image = models.ImageField(upload_to='post_pics',blank=True) 
    likes = models.ManyToManyField(User,blank=True,related_name='post_likes') 

    class Meta: 
     ordering = ['-post_date'] 

    def publish(self): 
     self.save() 

    def get_absolute_url(self): 
     return reverse('index') 

    def likes_as_flat_user_id_list(self): 
     return self.likes.values_list('id', flat=True) 

    def __str__(self): 
     return self.title 

更新 Like按钮视图(在饲料中的应用程序):

class LikePostToggle(RedirectView): 
    def get_redirect_url(self,pk): 
     obj = get_object_or_404(UserPost,pk=pk) 
     user = self.request.user 
     if user.is_authenticated(): 
      if user in obj.likes.all(): 
       obj.likes.remove(user) 
      else: 
       obj.likes.add(user) 
       notification = UserNotification.objects.create(
        user = self.request.user, 
        post = obj, 
        notify_type = "like", 
       ) 
     return obj.get_absolute_url() 

类似按钮的工作原理,所以我没有表现出任何的进口

回答

1

你应该让你的代码更容易,通知系统太简单了。 如果要通知用户,当有人像他后,你可以做到这一点的,例如查看

def like(request, post): 
    post.likes += 1 
    post.save() 
    notify = post.author 
    Notifications_model.objects.create(fromUser=request.user, toUser=notify, content="liked your post") 

然后在模板中可以遍历不是这样的声明:

{% for notification in filterd_notifications%} 
<p>{{notification.fromUser}} {{notification.content}}</p> 
{% endfor %} 
filterd_notifications是 notifications.objects.filter(toUser=request.user)
+0

我的观点是哪一个视图替换(或添加到)这个新视图? – Garrett

+0

'+ ='在这里做什么?我查了一下,但我并不真正了解它...... – Garrett

+0

嗨加勒特当我使用*喜欢+ = 1 *这是相同的*喜欢=喜欢+ 1 *这会增加一个喜欢的值,但如果你不知道什么剂量,这意味着我认为你应该在django之前先练习纯python:) –

相关问题