djangodjango-modelsdjango-viewsdjango-formsdjango-taggit

everytime i do some activity on my website i get this error :- Wallpaper.models.Wallpaper.DoesNotExist: Wallpaper matching query does not exist


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)

error

C:\Users\Atharva thaware\Desktop\aman\projects\Ongoing\WallpaperTown\WallpaperTown\Wallpaper\views.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:\Users\Atharva thaware\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)   File "C:\Users\Atharva thaware\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "C:\Users\Atharva thaware\Desktop\aman\projects\Ongoing\WallpaperTown\WallpaperTown\Wallpaper\views.py", line 55, in download
    wallpaper = Wallpaper.objects.get(name=wallpaper_name)   File "C:\Users\Atharva thaware\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)   File "C:\Users\Atharva thaware\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "C:\Users\Atharva thaware\Desktop\aman\projects\Ongoing\WallpaperTown\WallpaperTown\Wallpaper\views.py", line 55, in download
    wallpaper = Wallpaper.objects.get(name=wallpaper_name)   File "C:\Users\Atharva thaware\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)   File "C:\Users\Atharva thaware\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.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

I think there are two errors

1st one

Pagination may yield inconsistent results with an unordered object_list: <class 'Wallpaper.models. Wallpaper'> QuerySet.

2nd 0ne

Wallpaper.models.Wallpaper.DoesNotExist: Wallpaper matching query does not exist.


Solution

  • You have 1 error and 1 warning in your log. Let's discuss your warning first...

    "Pagination may yield inconsistent results with an unordered object_list: <class 'Wallpaper.models. Wallpaper'> QuerySet."

    This warning message is occurring because you haven't defined any ordering on your queryset (the order_by command you set up only works when there is a value in your ordering variable). So each time your page gets re-rendered the results will occur in random sequence. To fix this add order_by('field_name') at the end of your queryset or in the metaclass of your model.

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

    Note: This will not disturb the ordering you are applying here

    if ordering:
            wallpapers = wallpapers.order_by(ordering)
    

    The 2nd error is due to your get command. Because the get command throws an error if it can't find data in your database table. You should use exception handling to avoid it.

    View.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)