Код: Выделить всё
Using the URLconf ... Django tried these URL patterns, in this order:
...
detail/ [name='processor_detail']
The current path, detail/PayPal, matched the last one.
Код: Выделить всё
views.py
Код: Выделить всё
class ProcessorDetailView(DetailView):
model = Processor
template_name = 'finder/processor_detail.html'
slug_field = 'slug' # Tell DetailView to use the `slug` model field as the DetailView slug
slug_url_kwarg = 'slug' # Match the URL parameter name
Код: Выделить всё
models.py
Код: Выделить всё
class Processor(models.Model): #the newly created database model and below are the fields
name = models.CharField(max_length=250, blank=True, null=True) #textField used for larger strings, CharField, smaller
slug = models.SlugField(max_length=250, blank=True)
...
def __str__(self): #displays some of the template information instead of 'Processot object'
if self.name:
return self.name[0:20]
else:
return '--no processor name listed--'
def get_absolute_url(self): # new
return reverse("processor_detail", args=[str(self.slug)])
def save(self, *args, **kwargs): #`save` model a certain way(detailed in rest of function below)
if not self.slug: #if there is no value in `slug` field then...
self.slug = slugify(self.name) #...save a slugified `name` field value as the value in `slug` field
super().save(*args, **kwargs)
Код: Выделить всё
urls.py:
path("detail/", views.ProcessorDetailView.as_view(), name='processor_detail')
Подробнее здесь: https://stackoverflow.com/questions/791 ... nsensitive