Reputation: 167
With a small word like "cool" it works but if it's like "not cool" then it doesn't work because slug makes it not-cool.
path('<slug>/', views.series_pg, name='series_detail'),
#MODELS.PY
class Series(models.Model):
name = models.CharField(max_length=128, unique=True)
genre = models.CharField(max_length=128, default=1)
tv_or_movie = models.CharField(max_length=128, default=1)
period = models.CharField(max_length=128, default=1)
descritpion = models.TextField()
slug = models.SlugField(unique=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return "/%s/" %self.slug
#VIEWS.PY
def series_pg(request, slug):
series = Series.objects.get(name=slug)
If slug changes the original word then it doesn't work
EDIT:
My error is
DoesNotExist at /office/ Series matching query does not exist.
I added "The Office" but slug makes it office
Upvotes: 0
Views: 330
Reputation: 6404
Django slug field works like if you give value not cool
then it's slugify this to not-cool
.
In your views.py you want to filter by name
.
Say in the name it has the value not cool
but in slugfield, you keep the value not-cool
. Then you try to filter it out by .get(name=slug)
that means .get(not cool=not-cool)
. So the queryset doesn't return any matching object and doesn't match with the URL.
You can do it
def series_pg(request, slug):
series = Series.objects.get(slug=slug)
Upvotes: 1
Reputation: 116
"A slug is a short label for something, containing only letters, numbers, underscores or hyphens. They’re generally used in URLs."
-Django Documentation
What context are you using the Slug Field? It will convert spaces like "not cool" to "not-cool" because that's its purpose.
Upvotes: 0