Return more than one Django data list

Asked

Viewed 152 times

1

I made a homepage using Jango, I am loading my slides and menus dynamically and it was perfect. I put both the menu and the footer through include and it worked 100%.

Now I came across a problem, the menu of products loaded on the main page when I click on it is directed to a specific page of that product, it perfectly takes the product data and everything else, the only problem is that the loading of the products inside my menu, even being in the include it loses the data, as I solve it ?

view py.

def list_product(request, template_name='home/index.html'):
    # product = Product.objects.all()
    product = Product.objects.filter(status='True')
    products = {'list_products': product}
    return render(request, template_name, products)

def about_product(request, pk, template_name='home/product.html'):
    about_product = get_object_or_404(Product, pk=pk)
    return render(request, template_name, {'about_product': about_product})
  • 1

    Hello, @Marcelolop3s. You can also show your template code?

1 answer

0

I think a good option would be for you to separate the contexts.

def list_product(request, template_name='home/index.html'):
    menu_all_products = Product.objects.all()
    page_active_products = Product.objects.filter(status='True')
    context = {
        'menu_all_products': menu_all_products,
        'page_active_products', page_active_products,
    }
    return render(request, template_name, context)

Then in your template you will access in the menu the products that should appear there, and in the listing page your products that used in .filter(status='True')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.