skip to Main Content

my views.py

from django.shortcuts import render, HttpResponse,redirect
from .models import *


# Create your views here.

def index(request):
    products=Product.objects.all()
    params={'product':products}

    categories=Category.objects.all()
    category={'category':categories}

    return render(request,'index.html',params,category)

i am creating an ecommerce website on django and i want to pass two context in index.html one is product and one is category but after doing this the index.html is not working

2

Answers


  1. You can put two items inside a dictionary. Instead of

    products=Product.objects.all()
    params={'product':products}
    
    categories=Category.objects.all()
    category={'category':categories}
    

    You can do this:

    context = {'product': products, 'category': categories}
    

    And then pass the context dict:

    return render(request,'index.html',context)
    
    Login or Signup to reply.
  2. You merge the two dictionaries, so:

    def index(request):
        products = Product.objects.all()
        params = {'product': products}
    
        categories = Category.objects.all()
        category = {'category': categories}
    
        return render(request, 'index.html', {**params, **category})
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search