Javascript .save()不会更改字段django的值
我正在尝试实现一个通知系统。当用户单击下拉框中的一个通知时,我将使用AJAX Post请求修改布尔字段,以指示该通知的特定实例以前已被读取 这是我的密码: 这是我的HTML模板:Javascript .save()不会更改字段django的值,javascript,django,ajax,Javascript,Django,Ajax,我正在尝试实现一个通知系统。当用户单击下拉框中的一个通知时,我将使用AJAX Post请求修改布尔字段,以指示该通知的特定实例以前已被读取 这是我的密码: 这是我的HTML模板: <ul class="dropdown-menu dropdown-menu-right myDropDown"> {%for noti in notifications%} {{noti}} <li> <a href="#" class="top-te
<ul class="dropdown-menu dropdown-menu-right myDropDown">
{%for noti in notifications%}
{{noti}}
<li>
<a href="#" class="top-text-block" id="{{noti.id}}" onClick="return booleanchanger(this.id);">
<div class="top-text-heading">{{noti.object_type}}</div>
<p class = 'text-muted'><small>{{noti.time}}</small>></p>
<div class="top-text-light">{{noti.message}}</div>
</a>
</li>
{%endfor%}
</ul>
这是我的通知模型:
class Notifications(models.Model):
time = models.DateTimeField(auto_now=True)
target = models.ForeignKey(User , on_delete=models.CASCADE)
message = models.TextField()
object_type = models.CharField(max_length=100)
object_url = models.CharField(max_length=500,default = 'test')
is_read = models.BooleanField(default=False)
这是我处理ajax请求的视图:
def ReadNotificationView(request):
if request.method=='POST' and request.is_ajax():
pk = request.POST.get('pk',False)
obj = Notifications.objects.get(pk=pk)
obj.if_read = True
obj.save()
print(obj.if_read)
return JsonResponse({'status':'Success', 'is_read': 'changed'})
else:
return JsonResponse({'status':'Fail', 'is_read':'not changed'})
这是url.py:
path('notification/update/' , views.ReadNotificationView , name = 'read-notification')
从我的视图中打印(obj.if_read)会返回以下内容:
真的
但是,进入django管理页面并检查is_read字段的状态表明代码不起作用。有人能解决这个问题吗?我将不胜感激 打字错误。在模型中,您得到的
是在视图中读取的如果读取哇,谢谢您,这真是太傻了。。。
path('notification/update/' , views.ReadNotificationView , name = 'read-notification')