Discussion:
context must be a dict rather than Context.
Al Scham
7 years ago
Permalink
*Hi,*

*Im a new user going through this tutorial at OverIQ
: https://overiq.com/django/1.10/loading-templates-in-django*

*I've followed the instructions to the letter but it keep throwing the
Typeerror you see in the subject.*

*Here is a copy paist of my views :*

from django.shortcuts import render
from django.http import HttpResponse
from django import template
import datetime

def index(request):
return HttpResponse("Hello Django")

def today_is(request):
now = datetime.datetime.now()
t = template.loader.get_template('blog/datetime.html')
c = template.Context({'now': now})
html = t.render(c)
return HttpResponse(html)

*and here is a copy paist of my template:*

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Current Time</title>
</head>
<body>

{#This is a comment #}
{check the existence of now variable in the template using if tag #}
{% if now %}
<p>Current date and time is {{ now }}</p>
{% else %}
<p>now variable is not available</p>
{% endif %}

</body>
</html>

*Any help would be greatly appreciated.*

*Thanks*
*Allon*
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/67a732ee-ba22-4386-9241-1bd6c362885d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Constantine Covtushenko
7 years ago
Permalink
Hi Al,

I believe that an error you mentioned is thrown from line:

html = t.render(c)

As said in the error: context must be a dict
So just change

c = template.Context({'now': now})
to be
c = {'now': now}

For more information please check that documentation page
<https://docs.djangoproject.com/en/2.0/topics/templates/#django.template.backends.base.Template.render>

I hope that makes sense.

Regards,
Constantine C.
...
--
Sincerely yours,
Constantine C
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK52boVbcSATF%3DtSMLPbmA8LdWpjcA5gbuQvRUzUYtzv0UdhEw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...