Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

260
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda