I have recently switched from Django 1.9 to 1.11.17 and one thing is bothering me a lot. There is this error that says
TypeError at /somepath
context must be a dict rather than Context
The line that is throwing it is:
return render(request=request, template_name="mytemplate.html", context={"form": form, "update": updateType})
There are many answers on SO where people use RequestContext or Context instead of dict for context
and switching to dict solves their problem. But not for me. Here I am pretty sure that my context
is in fact a dict. What is interesting if I change it to:
return render(request=request, template_name="mytemplate.html", context={})
The error goes away, but obviously causes another error later on. Do you guys have any idead on what am I doing wrong here?
EDIT: My imports:
from django.shortcuts import render, render_to_response
from django.template.context import RequestContext, Context
I have tried bot render
and render_to_response
with similar effect. Also using Context or RequestContext gave similar error.
EDIT2: More code for reference
from django.http import (
HttpResponseRedirect,
HttpResponseBadRequest,
)
from django.shortcuts import render, render_to_response
from django.template import RequestContext, Context
from django.utils.html import escape
# some more imports, but from local files, not django
def update_my_template(request):
user = request.user
# preform some checks for user
...
if request.method == "GET":
updateType = request.GET.get("id")
if updateType:
form = None
if updateType == "something":
form = SomeForm(user)
if updateType == "something else":
form = DifferentForm()
if form is None:
return HttpResponseRedirect("/somepage")
# This was the code that worked in 1.9
rctx = RequestContext(
request, {"form": form, "update": updateType}
)
return render_to_response("mytemplate.html", rctx)
# some different cases, but the error is thrown already
...
Neither of these work:
dictctx = {"form": form, "update": updateType}
return render(request=request, template_name="mytemplate.html", dictctx)
.
ctx = Context({"form": form, "update": updateType})
return render(request=request, template_name="mytemplate.html", ctx)
.
ctx = Context({"form": form, "update": updateType})
return render(request=request, template_name="mytemplate.html", ctx.flatten())
.
rctx = RequestContext(request, {"form": form, "update": updateType})
return render_to_response("mytemplate.html", rctx.flatten())