[Fixed]-Django 1.9: render cannot pass context to my template

1👍

Show us your setting, you should have a folder “template”, and inside the html

Example settings.py

BASE_DIR = os.path.dirname(os.path.realpath(__file__))
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'template'),],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'trabajo.context_processors.globales',
                'trabajo.context_processors.menu',
                'trabajo.context_processors.globales',
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.core.context_processors.static',
            ],
        },
    },
]

you view.py:

def sellmainpage(request):
    products=Product.objects.all()
    context = {
        "products": products,
    }
    template = "template/sell.html"
    render_to_response(template,context,context_instance=RequestContext(request))

sell.html

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html> 
👤Oztro

0👍

instead of

return render(request, "sell.html", context)

try

loader.get_template('sell.html')
return HttpResponse(template.render(context,request))

don’t forget to import needed things.

from django.template import loader
from django.http import HttpResponse

Leave a comment