django 预填充字段用连字符分隔
我开始在我的管理员中添加 prepopulated_fields 选项,有趣的事情开始发生 我有这个模型
class Pelicula(models.Model):
nombre = models.CharField(max_length=50)
slug = models.SlugField(max_length= 15, unique= True, help_text = "Nombre corto para la URL", primary_key= True)
,在 admin.py 中
class PeliculaAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug' : ['nombre']}
没有什么花哨的,它在各处都有描述 但是,一旦我在 object_detail 通用视图中使用此 slug,该 slug 仅当它一开始只有一个单词时才起作用。 因此,如果我有这个视图
def detalle_pelicula(request, pelicula):
return list_detail.object_detail(
request,
queryset = Pelicula.objects.all(),
slug = pelicula,
template_name='sections/detalle_pelicula.html',
template_object_name = 'pelicula',
extra_context = extra_context,
)
,如果原始名称中有任何空格,我会得到“没有页面与给定查询匹配”。错误。所以详细信息/测试可以工作,但详细信息/测试页面不会 我有点难住了
I started putting prepopulated_fields options in my admins and funny stuff started happening
I have this model
class Pelicula(models.Model):
nombre = models.CharField(max_length=50)
slug = models.SlugField(max_length= 15, unique= True, help_text = "Nombre corto para la URL", primary_key= True)
and this in admin.py
class PeliculaAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug' : ['nombre']}
nothing fancy, it's described all over the place
But as soon as I use this slug in an object_detail generic view, the slug only works if it was only one word to begin with.
so if i have this view
def detalle_pelicula(request, pelicula):
return list_detail.object_detail(
request,
queryset = Pelicula.objects.all(),
slug = pelicula,
template_name='sections/detalle_pelicula.html',
template_object_name = 'pelicula',
extra_context = extra_context,
)
if the original name had any spaces in it, i get a "No Page matches the given query." error. So detail/test will work but detail/test-page won't
I'm a bit stumped
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你的 urlconf 设置使用 \w+ 解析你的 slug 尝试使用 [-A-Za-z0-9_]+
i'm guessing you have your urlconf setup parsing your slug using \w+ try using [-A-Za-z0-9_]+