django prepopulated fields break with hyphens

≯℡__Kan透↙ 提交于 2019-12-13 00:18:44

问题


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


回答1:


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'),


来源:https://stackoverflow.com/questions/5231593/django-prepopulated-fields-break-with-hyphens

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!