Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

252
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda