簡體   English   中英

Django 為什么會收到此錯誤找不到與查詢匹配的用戶配置文件

[英]Django why getting this error No user profile found matching the query

我正在嘗試實現加載更多按鈕,因此我正在為加載 json 數據編寫此視圖,但我不明白為什么會出現此錯誤。 No user profile found matching the query Raised by: members.views.UserProfileUpdate

這是我的觀點:

class PostJsonListView(View):
    def get(self, *args, **kwargs):
        print(kwargs)
        upper = kwargs.get('num_posts')
        lower = upper - 3 
        posts = list(Blog.objects.values()[lower:upper])
        posts_size = len(Blog.objects.all())
        max_size = True if upper >= posts_size else False
        return JsonResponse({'data': posts, 'max': max_size}, safe=False)

這是我的博客應用 urls.py

path('posts-json/<int:num_posts>',PostJsonListView.as_view(),name='json-view')

這是我的會員應用 urls.py

path('<slug:slug>/', UserProfileUpdate.as_view(), name='update-profile'),

如果我輸入了這樣的錯誤網址http://127.0.0.1:8000/sassassdsadasdd/給我同樣的錯誤No user profile found matching the query Raised by: members.views.UserProfileUpdate

您在UserProfileUpdate視圖中,使用slug獲取配置文件對象,如下所示:

Profile.objects.get(username=slug)

但! 您應該使用get_object_or_404快捷功能。

from django.shortcuts import get_object_or_404
get_object_or_404(Profile, username=slug)

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM