模型.py
class Tag(models.Model):
name = models.CharField(max_length=64, unique=True)
slug = models.SlugField(max_length=255, unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Tag, self).save(*args, **kwargs)
网址.py
url(r'^tag/(?P<slug>[A-Za-z0-9_\-]+)/$',
TagDetailView.as_view(),
name='tag_detail'),
视图.py
class TagDetailView(DetailView):
model = Tag
template_name = 'tag_detail_page.html'
context_object_name = 'tag'
这给了我一个 404:
Page not found (404)
http://localhost:9999/tag/RandomTag/
No tag found matching the query
为什么 Django 无法根据 slug 字段获取正确的对象?