【发布时间】:2020-08-13 15:42:34
【问题描述】:
所以我为我的用户模型制作了这个 ajax 视图
def new_notification(request):
user = request.user
user.profile.notifications += 1
user.save()
return JsonResponse(serializers.serialize('json', [user]), safe=False)
我使用 Integerfield 通知扩展了我的用户模型,但是当我调用 ajax 时,它并没有给我的通知模型 +1,有人知道发生了什么吗?
我的 urls.py
url(r'^ajax/new_notification/$', new_notification),
还有我的 ajax 调用
$.get('/ajax/new_notification/')
我的用户配置文件模型
class ProfileImage(models.Model):
"""
Profile model
"""
user = models.OneToOneField(
verbose_name=_('User'),
#to=settings.AUTH_USER_MODEL,
to = User,
related_name='profile',
on_delete=models.CASCADE
)
avatar = models.ImageField(upload_to='profile_image')
notifications = models.FloatField(default='0')
【问题讨论】:
-
如果您通过 ajax 执行此操作,
request.user是匿名的。您可以在模板中收集用户,然后将其发送到您的函数:def new_notification(request, user_id): -
@GAEfan 我该怎么做。我是 Django 新手
-
显示你的ajax调用代码。
-
@GAEfan 现在我的观点和 ajax 调用在我的问题中
标签: python django ajax django-views