Django 1.11.17 TypeError: 'context must be a dict rather than Context', except IT IS A DICT

django version 1.11 29
django 1.11 18
django 1.11 28 release notes
django 1.11 16
django versions
django 1.11 29
django 1.11 27 documentation
django 1.11 24

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())

The render logic is different, depending on what you pass to render:

def render(self, context):
        "Display stage -- can be called many times"
        with context.render_context.push_state(self):
            if context.template is None:
                with context.bind_template(self):
                    context.template_name = self.name
                    return self._render(context)
            else:
                return self._render(context)

and it looks as though you may be able to change your parameter template_name to just be name but your object doesn't have a context.render_context value which is why it would be better to create and use an instance of a Context

https://docs.djangoproject.com/en/1.11/_modules/django/template/base/#Template.render

The docs show passing an actual instance of a Context so I recommend that you do that in your code instead of just passing a dict:

>>> from django.template import Context, Template
>>> template = Template("My name is {{ my_name }}.")

>>> context = Context({"my_name": "Adrian"})
>>> template.render(context)
"My name is Adrian."

>>> context = Context({"my_name": "Dolores"})
>>> template.render(context)

so the easiest way to fix your code would be something like this:

from django.template import Context
...
return render(request=request, template_name="mytemplate.html", context=Context({"form": form, "update": updateType}))

Django 1.11.17 release notes | Django documentation, The Django Software Foundation deeply values the diversity of our developers, users, and community. We are distraught by the suffering,  Django 1.11.17 fixes several bugs in 1.11.16 and adds compatibility with Python 3.7. Bugfixes ¶ Prevented repetitive calls to geos_version_tuple() in the WKBWriter class in an attempt to fix a random crash involving LooseVersion since Django 1.11.14 ( #29959 ).

Ok, after some more digging (in "unresolved" questions) I found this gem. And yep, that was solution to my problem. Basically I had the line {{form|bootstrap}} in my mytemplate.html which was causing this.

Even better, updating the django-bootstrap-form to version 3.4 allowed me to keep the {{form|bootstrap}} and get rid of the error.

Django 1.11 release notes | Django documentation, Python compatibility¶. Django 1.11 requires Python 2.7, 3.4, 3.5, 3.6, or 3.7 (as of 1.11.17). We highly recommend and only officially  An issue was discovered in Django 1.11.x before 1.11.23, 2.1.x before 2.1.11, and 2.2.x before 2.2.4. If django.utils.text.Truncator's chars() and words() methods were passed the html=True argument, they were extremely slow to evaluate certain inputs due to a catastrophic backtracking vulnerability in a regular expression.

Always pass the variables/values in parameters. But You give both at a same time. Try this as,...

return render(request=request, template_name="mytemplate.html", {"form": form, "update": updateType})

Or

context={"form": form, "update": updateType} return render(request=request, template_name="mytemplate.html",context)

Full Support for Django 1.11 and Django 2.0 · Issue #436 · deschler , From Django 1.8, modeltranslation does not seem to follow Django's TypeError​: _clone() got an unexpected keyword argument '_populate'  Hello, I just tested todeay’s update to Django 1.11 and noticed that I get an exception when there’s a form with a CKEditor from django-ckeditor:

Django 1.11 loads widget2.html · Issue #63 · neutronX/django , Specs: Python 3.5.2 Django 1.11 Markdownx 2.0.17 (from pypi) I´m trying to markdownx.js:633 Uncaught TypeError: Cannot read property  When running autcomplete-light 2.3.3 with the latest version of six @ 1.11, you will get the following error. TypeError: Error when calling the metaclass bases

Change log, model != QuerySet.Model . Raise TypeError when incorrect types are used for Table.Meta  Django 1.11 requires Python 2.7, 3.4, 3.5, 3.6, or 3.7 (as of 1.11.17). We highly recommend and only officially support the latest release of each series. The Django 1.11.x series is the last to support Python 2.

Install Python 3.8, Virtual Environments using Pipenv, Django 3+ on , You can install things like Django , Requests , PyTorch , Tensorflow , and much more. The first part of this line 9, in <module> TypeError: 'module' object is not callable. Tell me what 'pip install django==1.11.17'. Then try  Django 1.11.18 fixes a security issue in 1.11.17. CVE-2019-3498: Content spoofing possibility in the default 404 page ¶ An attacker could craft a malicious URL that could make spoofed content appear on the default page generated by the django.views.defaults.page_not_found() view.

Comments
  • Just to make sure my assumptions are correct, can you edit your question and add your imports for Context and render ?
  • Sure, one second
  • Does my code below work, if you change from django.template.context to from django.template ?
  • The from django.template.context or from django.template seem to not make any difference unfortunately
  • I have tried this with no effect: ctx = Context({"form": form, "update": updateType}) return render(request=request, template_name="mytemplate.html", context=ctx)
  • Have you seen this ? It basically says to call .flatten() on any of your RequestContext objects before passing them to render
  • Yep, tried other answers already before asking mine :( Same error
  • I suppose it must be some super silly mistake, because I have literally tried every possibility, and my code looks identical to the ones posted in other SO answers.
  • Could you edit your answer and add as much code as you can from that file? The only thing I can think of is that it may be something else in the file
  • I don't see the difference between your first solution and mine. Also I have tried the second one already, with no difference :/
  • @Devligue In my first solution I just passed tuple, whereas in my second one I just passed variable of tupletype, But unfortunately you passed both at a same time. It’s a logical error
  • But you do see that you pass positional argument after keyword argument? This code can't be even run in python, it immediately throws me error