0

urls.py

from django.conf.urls import url
from system import views

app_name = 'project'

urlpatterns = [
    ...
    url(r'^cust/([\w-]+)/$',views.PublisherBookList.as_view()),
    ...
]

views.py

from . import models

class PublisherBookList(ListView):

    def get_queryset(self):
        self.name = get_object_or_404(Customer, name=self.args[0])
        return Customer.objects.filter(name=self.name)

models.py

class Customer(models.Model):
    name = models.CharField(max_length=255)

I do visit http://127.0.0.1:8000/project/custo/customername/

got error name 'Customer' is not defined

whats i missed here?...

Budi Gunawan
  • 101
  • 9

1 Answers1

2

You need to import Customer in your views.py

from .models import Customer 
Maxim Kukhtenkov
  • 734
  • 1
  • 7
  • 20
  • already did, i have lot of model. just error on views with `def get_queryset(self):` part. – Budi Gunawan May 14 '18 at 15:58
  • You imported the module, but not the class. You can either change the import to `from .models import Customer` or use `models.Customer` everywhere instead of just `Customer` – Maxim Kukhtenkov May 14 '18 at 16:02
  • ah gotcha. forgot do `models.` its work now, but nothing result. i check my db name `John`, put John on url and its show nothing. – Budi Gunawan May 14 '18 at 16:22
  • By default Django detail view uses `pk` url kwarg, which is Django database ID of the object. If you want to use custom slug attr you need to add few things. Here's relevant question: https://stackoverflow.com/questions/46815655/django-using-slug-field-for-detail-url – Maxim Kukhtenkov May 14 '18 at 16:27
  • Here it is explained how to alter default slug field https://stackoverflow.com/questions/5780803/how-to-specify-something-other-than-pk-or-slug-for-detailview/ – Maxim Kukhtenkov May 14 '18 at 16:44