Regular expression for django url with slug containing "/"


The idea is to create a regular expression to configure the url

I have 2 models, category and articles. The article model has a FK to category and the slug makes the join correctly slug (category) / slug (article). I want to use the separator "/" in the url if I use any other example "-" I have no problems.


class Categoria(models.Model):
nombre = models.CharField(max_length=25, unique=True, null=False)

def __unicode__(self):
    return self.nombre

class Articulo(models.Model):
    nombre = models.CharField(max_length=30)
    slug = models.SlugField(editable=False)
    descripcion = models.TextField(blank=True)
    precio= models.DecimalField(decimal_places = 2, default=0.00, max_digits = 12)
    categoria = models.ForeignKey(Categoria)
    destacado = models.BooleanField(default=False)
    orden = models.IntegerField(default = 0)
    image1 = models.ImageField(upload_to='article_img', blank=True)
    image2 = models.ImageField(upload_to='article_img', blank=True)
    image3 = models.ImageField(upload_to='article_img', blank=True)
    image4 = models.ImageField(upload_to='article_img', blank=True)
    image5 = models.ImageField(upload_to='article_img', blank=True)

    def save(self, *args, **kwargs):
        self.slug = '/'.join((slugify(self.categoria).lower().replace(' ','_'), slugify(self.nombre))).lower().replace(' ','_')
        super(Articulo, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.slug


urlpatterns = [
url(r'^$', ArticleList.as_view(), name='list'),
url(r'^(?P<slug>[\w-]+)/$', ArticleDetail.as_view(), name='detail'),


> Page not found (404)
> Request Method:   GET
> Request URL:  http://localhost:8000/tienda/ortopedia/muletas-de-aluminio
> Using the URLconf defined in sinam.urls, Django tried these URL patterns, in this order:  
> ^tienda/ ^$ [name='list']  
> ^tienda/ ^(?P<slug>[\w-]+)/$ [name='detail']  
> ^admin/
asked by Rodrigo Sangorrin 23.08.2017 в 05:13

1 answer


They handed me the solution through another forum.


url(r'^(?P<slug>[/\w-]+)/$', ArticleDetail.as_view(), name='detail'),
answered by 24.08.2017 в 01:00