1

I don't understand the Traceback of the error, but Server is running fine (0 problems), and I get all the data I needed from the view.py. Seems it has to be with the get() method...

DoesNotExist: matching query does not exist enter image description here

This are the models I used.

from django.contrib.auth.models import AbstractUser
from django.db import models


class User(AbstractUser):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=64, blank=True)
    image = models.ImageField(upload_to="portraits", blank=True, default="default.jpg")

    def __str__(self):
        return f"{self.username}"


class Category(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=64)
    description = models.CharField(max_length=255, blank=True)

    def __str__(self):
        return f"{self.name}"


class Item(models.Model):
    id = models.AutoField(primary_key=True)
    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="inventory")
    name = models.CharField(max_length=64)
    description = models.CharField(max_length=255)
    image = models.ImageField(blank=True, default="Megamind.jpg")
    starting_bid = models.PositiveIntegerField(default=0)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, default="1", related_name="stuff")
    active = models.BooleanField(default=True)
    favorited = models.ManyToManyField(User, blank=True, related_name="favorites")

    def __str__(self):
        return f"{self.name} of {self.owner}"

and the related view.py file

def index(request):
    auctions = Item.objects.filter(active=True)
    context = {'auctions':auctions, 'title':'Active Listings'}
    return render(request, "auctions/index.html", context)


def category_view(request, category):
    category = category.capitalize()
    category_obj = Category.objects.get(name=category)
    category_id = category_obj.id
    items = Item.objects.filter(category=category_id, active=True)
    context = {'auctions':items, 'title':category}
    return render(request, "auctions/index.html", context)

If it helps: I'm passing from urls.py to view as str

path("<str:category>", views.category_view, name="category")
Nytswa
  • 37
  • 6
  • 1
    You realize that you capitalize the name of the category first, so it is possible that there is a category with name `'foo'`, not not one with `'Foo'`. – Willem Van Onsem Jan 19 '22 at 16:23
  • 1
    Try `Category.objects.filter(name__icontains=category).first()` – Ahtisham Jan 19 '22 at 17:08
  • Thx, but that was the idea (actually 'foo' doesn't work). Right now eveything on the server works normally. – Nytswa Jan 19 '22 at 17:20

2 Answers2

1

Take a look into traceback, the error is raised on /favicon.ico request. The modern browser automatically asks about favico.

You can do something like that to avoid this error:

 url(r'^favicon\.ico$',RedirectView.as_view(url='/static/images/favicon.ico')),
Sharpek
  • 658
  • 4
  • 5
1

As @Sharpek rightly mentioned, the error is caused by a /favicon.ico request, apparently from the browser. The easiest way to fix it is to add this to your base.html template into head tag:

{% load static %} <!-- If it wasn't already used -->

<link rel="shortcut icon" type="image/png" href="{% static 'favicon.ico' %}"/>

Then if you don't have a favicon.ico inside your STATIC_ROOT, you'll have a 404 error. Otherwise, you'll have a 200.

Yevgeniy Kosmak
  • 3,561
  • 2
  • 10
  • 26
  • I don't know where those favicon.ico came from. Is that as 'complement' to that url() @Sharpek mentioned? Thx btw – Nytswa Jan 20 '22 at 14:19
  • 1
    @Nytswa no, my solution is an alternative to Sharpek's. Both of our solutions make Django give the static file to the client. See [this](https://stackoverflow.com/a/40330875/12914274) answer for more information about how static files work in Django. – Yevgeniy Kosmak Jan 20 '22 at 14:35
  • Yeah I had all my ROOTs and URLs in my settings.py working. And eventhough I added the icon and have the 200, the icon still doesn't show up (even tried with png) jajaj... Anyways, that's another problem. Thank you for your time and patience. – Nytswa Jan 20 '22 at 15:27
  • @Nytswa good luck and glad to help :) `the icon still doesn't show up` could be the browser's cache, try it in incognito mode or on in different browser. – Yevgeniy Kosmak Jan 20 '22 at 16:43