Omar Gonzales
Omar Gonzales

Reputation: 4008

Django: DoesNotExist at /admin

I've a view that receives an argument to make a Query filter and show some results.

The URL for this view is:

views.ProdCatDetail

urlpatterns = [
    path('', views.allCat, name = 'allCat'),
    path('<slug:c_slug>', views.ProdCatDetail, name = 'ProdCatDetail'),
]

the problem is that if I want to access the admin panel, through:

http://127.0.0.1:8000/admin

I cannot becuase the view:

views.ProdCatDetail

gets called, and since there is no Category "admin" I get an error:

DoesNotExist at /admin
Category matching query does not exist.

How can avoid this, without using another URL for the views.ProdCatDetail view???

UPDATE 1:

This view filter the category model, and filters the product model to get all products corresponding to this category.

As you can see in the URL it accepts 1 parameter, that is: <slug:c_slug>, however, admin from http://127.0.0.1:8000/admin is considered as a slug, when it shouldn't because I only this this URI to enter the admin panel.

I've tried using and if stament to control this flow:

if c_slug is not "muestras" and not "admin"

But now I'm getting:

UnboundLocalError at /admin local variable 'category' referenced before assignment

def ProdCatDetail(request, c_slug):

    if c_slug is not "muestras" and not "admin":

        try:
            category = Category.objects.get(slug=c_slug)
            products = Product.objects.filter(category__slug=c_slug)
        except Exception as e:
            raise e

    return render(request, 'shop/productos_por_categoria.html', {'category': 
    category, 'products': products})

Upvotes: 0

Views: 368

Answers (1)

Hristiyan Bonev
Hristiyan Bonev

Reputation: 82

I think that you might fix this with adding explicit path for AdminSite in your URLconf. Try to add the following line above the path that has views.ProdCatDetail view.

path('admin/', admin.site.urls),

However keep in mind that you should add admin package with:

from django.contrib import admin

Upvotes: 2

Related Questions