Answer the question
In order to leave comments, you need to log in
`
How to nest url`s in Django?
Good afternoon.
Given: django 1.9
Can you tell me how to nest url in Django? Example:
I am currently using this construct
url(r'^articles/', views.articles, name='articles'),
url(r'^articles_item/(?P<slug>.+)/$', views.articles_detail, name='articles_detail'),
Answer the question
In order to leave comments, you need to log in
Dealt with nested urls.
I have articles, categories of articles and subcategories, I do this.
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'),
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})
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)])
<a href="{% url 'articles_subcategory' category_slug=i.category.slug slug=i.slug %}">{{ i.name }}</a>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question