Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
How to set specified kwargs to url outside for loop?

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)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 %}.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!