【问题标题】:Django JsonResponse: User matching query does not existDjango JsonResponse:用户匹配查询不存在
【发布时间】:2020-12-01 16:55:43
【问题描述】:

所以我尝试为我的模型制作一个 ajax,但它给了我一个匹配的查询不存在,这是完整的错误:

Internal Server Error: /messages/notification/
Traceback (most recent call last):
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\mixins.py", line 52, in dispatch
    return super().dispatch(request, *args, **kwargs)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\detail.py", line 106, in get
    self.object = self.get_object()
File "C:\Users\berna\Desktop\Python & Javascript\Web development\MiFamiliaEsUnDesastre\mifamiliaesundesastre\chat\views.py", line 26, in get_object
    obj, created    = Thread.objects.get_or_new(self.request.user, other_username)
File "C:\Users\berna\Desktop\Python & Javascript\Web development\MiFamiliaEsUnDesastre\mifamiliaesundesastre\chat\models.py", line 29, in get_or_new
    user2 = Klass.objects.get(username=other_username)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 415, in get
    raise self.model.DoesNotExist(
django.contrib.auth.models.User.DoesNotExist: User matching query does not exist.
HTTP GET /messages/notification/ 500 [0.16, 127.0.0.1:56012]

这也是我的代码: 模型.py

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')

views.py

def notifications(request, user_id, *args, **kwargs):
    user = User.objects.get(pk=user_id)
    user.profile.notifications = user.profile.notifications + 1
    user.save()
    return JsonResponse(data=user.profile.notifications, safe=False)

urls.py

path('messages/notification/', notifications)

我的 ajax 调用的 html

// Add the notification val
$.get('notification/')

有人告诉我那是我没有用户,但是当我有用户时,我不知道发生了什么

【问题讨论】:

  • 是的,同样的问题
  • 您没有在该调用中提供 user_id 参数,这就是原因
  • @KishanParmar 那么我是否需要摆脱 user_id 并只设置 pk=1?
  • 在 url 模式 + 中获取 user_id 的绑定,并在 views.py 参数的第一行中删除 user_id,然后使用 user=request.user 就可以了

标签: django ajax django-models django-views


【解决方案1】:

您需要向视图发送user_id

path('messages/notification/(?P<user_id>[a-zA-Z0-9/_\.-]*)', notifications)

和:

$.get('messages/notification/{{ user.pk }}')

更新

您必须知道他们的用户 ID 才能收到他们的通知:

$.get('messages/notification/12234')

我假设你在某处有一个用户模型?

另外,user_id 是以字符串形式出现的,所以需要 int() 那个:

user = User.objects.get(pk=int(user_id))

或使用:

path('messages/notification/<int:user_id>', notifications)

【讨论】:

  • 它不起作用,当我去消息/通知是当用户匹配查询不退出时
  • 您将不得不登录您的notification 函数以查看发生了什么。此外,user_id 以字符串形式出现,因此需要int()。查看更新
  • 我已经发布了一个新问题,你能帮帮我吗? stackoverflow.com/questions/63378988/…
  • 当前答案的解决方法是什么?
  • 但是如果我能解决问题stackoverflow.com/questions/63378988/…我可以解决这个问题
【解决方案2】:

所以对于后来看到这个的人来说,我所做的就是改变

path('messages/notification/', notifications)

url(r'^ajax/notification/$', notification),

【讨论】:

  • 从您发布的代码中无法知道这一点。
  • @GAEfan 是的,很抱歉,但非常感谢,我会给你一个很好的答案
猜你喜欢
  • 2015-03-13
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
相关资源
最近更新 更多