每次我在我的网站上做一些活动时,我都会收到这样的错误:-Wallpaper.models.Wallpaper.DoesN



Views.py

def home(request):
    WAllPAPER_PER_PAGE = 4
    WALL = Wallpaper.objects.all()
    from django.core.paginator import EmptyPage, Paginator
    from django.db.models import Q
    qd = request.GET.copy()
    qd.pop('page', None)
    querystring = qd.urlencode()
    #link formatting for ordering
    ordering =request.GET.get('ordering', "")
    #link formatting for sorting
    search = request.GET.get('search', "")
    if search:
        wallpapers = Wallpaper.objects.filter(Q(name__icontains=search) | Q(category__category_name__icontains=search) | Q(tags__tag__icontains=search)).distinct()
        WALL = None
    else:
        wallpapers = Wallpaper.objects.all()
    if ordering:
        wallpapers = wallpapers.order_by(ordering)
    page = request.GET.get('page', 1)
    wallpaper_paginator = Paginator(wallpapers, WAllPAPER_PER_PAGE)
    try:
        wallpapers = wallpaper_paginator.page(page)
    except EmptyPage:
        wallpapers = wallpaper_paginator.page(wallpaper_paginator.num_pages)
    except:
        wallpapers = wallpaper_paginator.page(WAllPAPER_PER_PAGE)
    context = {'querystring': querystring, "wallpapers": wallpapers, 'page_obj': wallpapers, 'is_paginated': True, 'paginator': wallpaper_paginator, 'WALL': WALL}
    return render(request, "Wallpaper/Home.html", context)

def All_category(request):
    Cat = Category.objects.all()
    context = {'Cat': Cat }
    return render(request, "Wallpaper/ALL_Category.html", context )

def category(request, Category_name):
    cat = Category.objects.get(category_name=Category_name)
    wallpapers = Wallpaper.objects.filter(category__category_name=Category_name)
    context = {'cat':cat, 'wallpapers': wallpapers}
    return render(request,'Wallpaper/Category.html', context)

def download(request, wallpaper_name):
    wallpaper = Wallpaper.objects.get(name=wallpaper_name)
    similar_wallpapers = wallpaper.tags.similar_objects()
    context = {'wallpaper': wallpaper, 'similar_wallpapers': similar_wallpapers}
    return render(request, 'Wallpaper/download.html', context)

错误

C:UsersAtharva thawareDesktopamanprojectsOngoingWallpaperTownWallpaperTownWallpaperviews.py:30: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'Wallpaper.models. Wallpaper'> QuerySet.   wallpaper_paginator = Paginator(wallpapers, WAllPAPER_PER_PAGE) [19/Jul/2022 22:29:39] "GET / HTTP/1.1" 200 10744 [19/Jul/2022 22:29:39] "GET /media/Wallpaper/Images/wp4589844-inosuke-hashibira-wallpapers.jpg HTTP/1.1" 304 0 [19/Jul/2022 22:29:39] "GET /media/Wallpaper/Images/wp2162463-shoto-todoroki-wallpapers.png HTTP/1.1" 304 0 [19/Jul/2022 22:29:39] "GET /media/Wallpaper/Images/wp2490700-haikyu-2018-wallpapers.jpg HTTP/1.1" 304 0 [19/Jul/2022 22:29:39] "GET /media/Wallpaper/Images/wp1850905-laferrari-wallpapers.jpg HTTP/1.1" 304 0 Not Found: /docs/5.1/assets/brand/bootstrap-logo.svg [19/Jul/2022 22:29:39] "GET /docs/5.1/assets/brand/bootstrap-logo.svg HTTP/1.1" 404 3058 Internal Server Error: /favicon.ico/ Traceback (most recent call last):   File "C:UsersAtharva thawareAppDataLocalProgramsPythonPython39libsite-packagesdjangocorehandlersexception.py", line 47, in inner
    response = get_response(request)   File "C:UsersAtharva thawareAppDataLocalProgramsPythonPython39libsite-packagesdjangocorehandlersbase.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "C:UsersAtharva thawareDesktopamanprojectsOngoingWallpaperTownWallpaperTownWallpaperviews.py", line 55, in download
    wallpaper = Wallpaper.objects.get(name=wallpaper_name)   File "C:UsersAtharva thawareAppDataLocalProgramsPythonPython39libsite-packagesdjangocorehandlersexception.py", line 47, in inner
    response = get_response(request)   File "C:UsersAtharva thawareAppDataLocalProgramsPythonPython39libsite-packagesdjangocorehandlersbase.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "C:UsersAtharva thawareDesktopamanprojectsOngoingWallpaperTownWallpaperTownWallpaperviews.py", line 55, in download
    wallpaper = Wallpaper.objects.get(name=wallpaper_name)   File "C:UsersAtharva thawareAppDataLocalProgramsPythonPython39libsite-packagesdjangodbmodelsmanager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)   File "C:UsersAtharva thawareAppDataLocalProgramsPythonPython39libsite-packagesdjangodbmodelsquery.py", line 435, in get
    raise self.model.DoesNotExist( Wallpaper.models.Wallpaper.DoesNotExist: Wallpaper matching query does not exist. [19/Jul/2022 22:29:42] "GET /favicon.ico/ HTTP/1.1" 500 73315

我认为有两个错误

第一个

分页可能会产生与无序对象列表不一致的结果:<类"壁纸.models.Wallpaper">QuerySet。

第二个0ne

壁纸.models.Wallpaper.DoesNotExist:壁纸匹配查询不存在。

日志中有1个错误和1个警告。让我们先讨论一下你的警告。。。

"分页可能会产生与无序对象列表不一致的结果:<类"壁纸.models.Wallpaper">QuerySet">

出现此警告消息是因为您尚未在查询集上定义任何排序(您设置的order_by命令仅在排序变量中有值时有效(。因此,每次重新渲染页面时,结果都将按随机顺序出现。要解决此问题,请在查询集的末尾或模型的元类中添加order_by('field_name'(。

查看.py

if search:
    wallpapers = Wallpaper.objects.filter(Q(name__icontains=search) | Q(category__category_name__icontains=search) | Q(tags__tag__icontains=search)).distinct().order_by('-pk')
    WALL = None
else:
    wallpapers = Wallpaper.objects.all().order_by('-pk')

注意:这不会干扰您在这里应用的订单

if ordering:
        wallpapers = wallpapers.order_by(ordering)

第二个错误是由于您的get命令造成的。因为get命令在数据库表中找不到数据时会抛出错误。您应该使用异常处理来避免它。

查看.py

def category(request, Category_name):
   try:
      cat = Category.objects.get(category_name=Category_name)
      wallpapers = Wallpaper.objects.filter(category__category_name=Category_name)
   except Exception as exc:
      cat = None
      wallpapers = None
      messages.error = (request, 'Sorry! data does not exist')
   context = {'cat':cat, 'wallpapers': wallpapers}
   return render(request,'Wallpaper/Category.html', context)
def download(request, wallpaper_name):
    try:
       wallpaper = Wallpaper.objects.get(name=wallpaper_name)
       similar_wallpapers = wallpaper.tags.similar_objects()
      
    except Exception as exc:
       wallpaper = None
       similar_wallpapers = None
       messages.error = (request, 'Sorry! data does not exist')
    context = {'wallpaper': wallpaper, 'similar_wallpapers': similar_wallpapers}
    return render(request, 'Wallpaper/download.html', context)

相关内容

  • 没有找到相关文章