slugfield不起作用,问题是基数为10的int()的无效文字: 我尝试所有的英语视频和一些法语视频
models
from django.utils.text import slugify
class Region(models.Model):
...
slug = models.SlugField(max_length=140, unique=True)
def __str__(self):
return self.name
def _get_unique_slug(self):
slug = slugify(self.name)
unique_slug = slug
num = 1
while Region.objects.filter(slug=unique_slug).exists():
unique_slug = '{}-{}'.format(slug, num)
num += 1
return unique_slug
def save(self, *args, **kwargs):
if not self.slug:
self.slug = self._get_unique_slug()
super().save(*args, **kwargs)
urls
path('<region_slug>', views.detail, name='detail'),
views
def detail(request,region_slug):
region=get_object_or_404(Region ,pk=region_slug)
context = {
.....
'region_slug':region.slug
}
答案 0 :(得分:0)
我认为您的问题在您看来。这条线指向模型的主键(pk),但pk与子弹头不同。 pk引用了自动插入的id
字段。
def detail(request, region_slug):
region = get_object_or_404(Region, pk=region_slug)
您可以将slug字段用作主键,但我建议您指向现在的slug字段。
def detail(request, region_slug):
region = get_object_or_404(Region, slug=region_slug)