django预先填充的字段用连字符分隔 [英] django prepopulated fields break with hyphens
问题描述
我开始在管理员中添加prepopulated_fields选项,有趣的事情开始发生
我有这个模型
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)
,并且在admin.py
and this in admin.py
class PeliculaAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug' : ['nombre']}
没什么好说的,它到处都有描述
但是,一旦我在object_detail通用视图中使用了该段,则该段仅在只有一词开始。
所以如果我有这个视图
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
推荐答案
我猜你有使用\w +解析slug的urlconf设置,尝试使用[-A-Za-z0-9 _] +
i'm guessing you have your urlconf setup parsing your slug using \w+ try using [-A-Za-z0-9_]+
# e.g. in urls.py
url(r'del/(?P<slug>[-A-Za-z0-9_]+)/$', 'person_delete', name='person_delete'),
这篇关于django预先填充的字段用连字符分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!