我有这个型号:
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)
它会记录用户对其他用户帖子的操作,以便通知帖子的所有者。
然后我有了这个观点:
class NotifyMarkRead(RedirectView):
def get_redirect_url(self,pk):
obj = get_object_or_404(UserNotification,pk=pk)
if obj.read != True:
obj.read == True
else:
obj.read == False
return obj.get_absolute_url()
这是处理用户点击通知的视图。此视图应检查read
是否等于True或False(默认情况下为false)。如果用户单击通知,则视图应将read
更新为True。但是,它没有这样做。那么当用户浏览此视图时,如何更新模型中的read
字段?
此外,我不同意该页面正在被访问,但它只是转到/notify/1/read/
而是重定向回/notify/
。请确定这是否重要。
以下是我的网址:
from django.conf.urls import url
from notify import views
app_name = 'notify'
urlpatterns = [
url(r'^$',views.UserNotifications.as_view(),name='user_notifications'),
url(r'^(?P<pk>\d+)/read/$',views.NotifyMarkRead.as_view(),name='user_notify_toggle'),
]
答案 0 :(得分:2)
obj.save()
尝试此操作以保存更新