【发布时间】:2020-08-11 15:47:18
【问题描述】:
所以我做了一个 django ajax,每次有新消息时,我添加到我的用户模型的通知对象都会是 +1,问题是我不知道为什么会给我这个错误:
django.contrib.auth.models.User.DoesNotExist: User matching query does not exist.
这是我的代码:
views.py:
def count_notification(request):
user = request.user
obj = user.profile.objects.get(pk=1)
obj.notifications = obj.notifications +1
obj.save()
response_data = {'success':1}
return HttpResponse(json.dumps(response_data), content_type="application/json")
urls.py:
path('messages/notification/', count_notification)
html文件js
// Add the notification val
$.get('notification/')
models.py:
class ProfileImage(models.Model):
"""
Profile model
"""
user = models.OneToOneField(
verbose_name=_('User'),
to=settings.AUTH_USER_MODEL,
related_name='profile',
on_delete=models.CASCADE
)
avatar = models.ImageField(upload_to='profile_image')
notifications = models.FloatField(default='0')
谢谢你的帮助
【问题讨论】:
-
它告诉你用户不存在,所以在这种情况下,
id等于1的User实例不存在,因此查询失败。 -
@lmiguelvargasf 那么我该如何修复它,我想更改为已登录用户的通知值
标签: javascript python django ajax