Разобрался с вложенными url.
У меня есть статьи, категории статей и подкатегории, делаю так.
urls.py
url(r'^articles/$', views.articles, name='articles'),
url(r'^articles/(?P<category_slug>[-\w]+)/(?P<slug>[-\w]+)/$', views.articles_subcategory, name='articles_subcategory'),
url(r'^articles/(?P<slug>.+)/$', views.articles_category, name='articles_category'),
views.py
def articles_category(request, slug):
articles_cat = get_object_or_404(ArticlesCategory, slug=slug)
return render(request, 'Articles/articles_category.html', {'articles_cat': articles_cat})
def articles_subcategory(request, slug, *args, **kwargs):
articles_subcat = get_object_or_404(ArticlesSubCategory, slug=slug)
return render(request, 'Articles/articles_subcategory.html', {'articles_subcat': articles_subcat})
models.py
class ArticlesCategory(models.Model):
...
def get_absolute_url(self):
return reverse('articles_category', args=[str(self.slug)])
class ArticlesSubCategory(models.Model):
...
def get_absolute_url(self):
return reverse('articles_subcategory', args=[str(self.category.slug), str(self.slug)])
template.html
<a href="{% url 'articles_subcategory' category_slug=i.category.slug slug=i.slug %}">{{ i.name }}</a>