Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

88
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)
8 months 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 %}.

8 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos