I have some links in template.
<ul>
{% for cat in cats %}
<li><a href="{% url 'c_index' cat.slug %}">{{ cat.name }}</a>
{% endear %}
</ul>
<ul>
{% for type in types %}
<li><a href="{% url 'ct_index' cat.slug type.slug %}">{{ type.name }}</a>
{% endear %}
</ul>
Of course, I can't access second link because I can't use 'cat.slug' outside {% for cat in cats %} loop.
But I want to set "cat.slug" to second link without using {% for cat in cats %} loop.
How can I do this? For example, using template tag?
url(r'^c_(?P<cat>[-\w]+)/$', views.c_index, name='c_index'),
url(r'^c_(?P<cat>[-\w]+)/t_(?P<type>[-\w]+)/$', views.ct_index, name='ct_index'),
def c_index(request, cat):
c = {}
posts = Post.objects.filter(category__slug=cat)
cats = Category.objects.all()
c.update({
'posts': posts,
'cats': cats,
})
return render(request, 'classifieds/index.html', c)
def ct_index(request, cat, type):
c = {}
posts = Post.objects.filter(category__slug=cat).filter(type=type)
cats = Category.objects.all()
types = Types.objects.all()
c.update({
'posts': posts,
'cats': cats,
'types': types,
})
return render(request, 'classifieds/index.html', c)
In your views, you need to add category = Category.objects.get(slug=cat)
. Then add that category
variable to your context.
Then in your template, you can access that category using the template variable that you defined in your context. If it was category
, your url tag will look like: {% url 'ct_index' category.slug type.slug %}
.