Home > Net >  Error during template rendering: 'set' object is not reversible after installing django-de
Error during template rendering: 'set' object is not reversible after installing django-de

Time:01-16

I created a new fresh django project. I created an app inside the django project and then tried to install django-debug-toolbar. I followed the instructions from the official docs. When I point to the app url I got Error during template rendering: 'set' object is not reversible

The files from the base project urls.py and settings.py were modified as follows:

#urls.py 
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('meetings/', include('meetings.urls')),
    path('__debug__/', include('debug_toolbar.urls'))
]
#settings.py
#...
DEBUG = True

#...
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'meetings',
    'debug_toolbar'
]

#...
MIDDLEWARE = [
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

#...
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

#...
INTERNAL_IPS = [
    '127.0.0.1'
]

The config files for the app "meetings" have the following configuration:

#urls.py 
from django.urls import path
from . import views

urlpatterns = {
    path('hello/', views.say_hello)
}
#views.py
from django.shortcuts import render
from django.http import HttpResponse

def say_hello(request):
    return render(request, 'hello.html', {'name': 'PE'})
<!--template/hello.html-->
<html>
<head>
<title>Welcome</title>
</head>
<body>

    {% if name %}
    <h1>Hello {{ name }}</h1>
    {% else %}
    <h1>Hello world</h1>
    {% endif %}

</body>
</html>

When I start the server and go to the url http://127.0.0.1:8000/meetings/hello I get the following error (I simplified the path to the .venv folder):

Error during template rendering

In template (...)/.venv/lib/python3.10/site-packages/debug_toolbar/templates/debug_toolbar/base.html, error at line 12

'set' object is not reversible

Here is some more context for this error:

Request Method: GET
Request URL: http://127.0.0.1:8000/meetings/hello/

Django Version: 4.1.5
Python Version: 3.10.6


Template error:
In template (...)/.venv/lib/python3.10/site-packages/debug_toolbar/templates/debug_toolbar/base.html, error at line 12
   'set' object is not reversible
   2 : {% block css %}
   3 : <link rel="stylesheet" href="{% static 'debug_toolbar/css/print.css' %}" media="print">
   4 : <link rel="stylesheet" href="{% static 'debug_toolbar/css/toolbar.css' %}">
   5 : {% endblock %}
   6 : {% block js %}
   7 : <script type="module" src="{% static 'debug_toolbar/js/toolbar.js' %}" async></script>
   8 : {% endblock %}
   9 : <div id="djDebug"  dir="ltr"
   10 :      {% if not toolbar.should_render_panels %}
   11 :      data-store-id="{{ toolbar.store_id }}"
   12 :      data-render-panel-url=" {% url 'djdt:render_panel' %} "
   13 :      {% endif %}
   14 :      {% url 'djdt:history_sidebar' as history_url %}
   15 :      {% if history_url %}
   16 :      data-sidebar-url="{{ history_url }}"
   17 :      {% endif %}
   18 :      data-default-show="{% if toolbar.config.SHOW_COLLAPSED %}false{% else %}true{% endif %}"
   19 :      {{ toolbar.config.ROOT_TAG_EXTRA_ATTRS|safe }}>
   20 :   <div  id="djDebugToolbar">
   21 :     <ul id="djDebugPanelList">
   22 :       <li><a id="djHideToolBarButton" href="#" title="{% trans 'Hide toolbar' %}">{% trans "Hide" %} »</a></li>


Traceback (most recent call last):
  File "(...)/.venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "(...)/.venv/lib/python3.10/site-packages/debug_toolbar/middleware.py", line 74, in __call__
    rendered = toolbar.render_toolbar()
  File "(...)/.venv/lib/python3.10/site-packages/debug_toolbar/toolbar.py", line 82, in render_toolbar
    return render_to_string("debug_toolbar/base.html", context)
  File "(...)/.venv/lib/python3.10/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "(...)/.venv/lib/python3.10/site-packages/django/template/backends/django.py", line 62, in render
    return self.template.render(context)
  File "(...)/.venv/lib/python3.10/site-packages/django/template/base.py", line 175, in render
    return self._render(context)
  File "(...)/.venv/lib/python3.10/site-packages/django/test/utils.py", line 111, in instrumented_test_render
    return self.nodelist.render(context)
  File "(...)/.venv/lib/python3.10/site-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "(...)/.venv/lib/python3.10/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "(...)/.venv/lib/python3.10/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "(...)/.venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 322, in render
    return nodelist.render(context)
  File "(...)/.venv/lib/python3.10/site-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "(...)/.venv/lib/python3.10/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "(...)/.venv/lib/python3.10/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "(...)/.venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 472, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "(...)/.venv/lib/python3.10/site-packages/django/urls/base.py", line 54, in reverse
    app_list = resolver.app_dict[ns]
  File "(...)/.venv/lib/python3.10/site-packages/django/urls/resolvers.py", line 633, in app_dict
    self._populate()
  File "(...)/.venv/lib/python3.10/site-packages/django/urls/resolvers.py", line 570, in _populate
    url_pattern._populate()
  File "/(...)/.venv/lib/python3.10/site-packages/django/urls/resolvers.py", line 543, in _populate
    for url_pattern in reversed(self.url_patterns):

Exception Type: TypeError at /meetings/hello/
Exception Value: 'set' object is not reversible

CodePudding user response:

urlpatterns in meetings/urls.py should be a list:

urlpatterns = [
    path('hello/', views.say_hello)
]
  • Related