Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError __str__ returned non-string (type PosixPath) /debug_toolbar/templates/debug_toolbar/panels/staticfiles.html #2002

Open
dougwmorrow opened this issue Sep 6, 2024 · 4 comments

Comments

@dougwmorrow
Copy link

dougwmorrow commented Sep 6, 2024

django-debug-toolbar: 4.4.6

I'm just providing a solution to an event I came across. Line 29 for /debug_toolbar/templates/debug_toolbar/panels/staticfiles.html needs to have {{ staticfile }} updated to {{ staticfile|stringformat:"s" }}

Below is the event that happened.

When running uvicorn project.asgi:application --host 127.0.0.1 --port 8001 and then loading 127.0.0.1:8001 in a new web browser, the TypeError str returned non-string (type PosixPath). Specifically focusing on {{ staticfile }}. I updated {{ staticfile }} to {{ staticfile|stringformat:"s" }} and that resolved the TypeError.

Thanks!

@tim-schilling
Copy link
Contributor

Can you share more information about your project? This isn't something we've come across elsewhere. We'd want to know what your static file was and what your installed apps and middlewares are?

@dougwmorrow
Copy link
Author

dougwmorrow commented Sep 11, 2024

Sure. After switching to ASGI and installing uvicorn, I noticed the event.

Legend:

  1. Apps/Middlewares
  2. Static File Previous
  3. Static File Updated

Look for the line {{ staticfile|stringformat:"s" }} is shown on.

  1. Apps/Middlewares:
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django_select2",
    "django_htmx",
    "crispy_forms",
    "crispy_tailwind",
    "storages",
    "widget_tweaks",
    "django_user_agents",
    "debug_toolbar",
    'django.contrib.gis',
    'leaflet',
    'channels',
]

MIDDLEWARE = [
    "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",
    "django_htmx.middleware.HtmxMiddleware",
    "django_user_agents.middleware.UserAgentMiddleware",
    "project.middleware.DeviceDetailsMiddleware",
    "debug_toolbar.middleware.DebugToolbarMiddleware",
    'whitenoise.middleware.WhiteNoiseMiddleware',
]
  1. Static File Previous:
{% load i18n %}

<h4>{% blocktrans count staticfiles_dirs|length as dirs_count %}Static file path{% plural %}Static file paths{% endblocktrans %}</h4>
{% if staticfiles_dirs %}
  <ol>
    {% for prefix, staticfiles_dir in staticfiles_dirs %}
      <li>{{ staticfiles_dir }}{% if prefix %} {% blocktrans %}(prefix {{ prefix }}){% endblocktrans %}{% endif %}</li>
    {% endfor %}
  </ol>
{% else %}
  <p>{% trans "None" %}</p>
{% endif %}

<h4>{% blocktrans count staticfiles_apps|length as apps_count %}Static file app{% plural %}Static file apps{% endblocktrans %}</h4>
{% if staticfiles_apps %}
  <ol>
    {% for static_app in staticfiles_apps %}
      <li>{{ static_app }}</li>
    {% endfor %}
  </ol>
{% else %}
  <p>{% trans "None" %}</p>
{% endif %}

<h4>{% blocktrans count staticfiles|length as staticfiles_count %}Static file{% plural %}Static files{% endblocktrans %}</h4>
{% if staticfiles %}
  <dl>
    {% for staticfile in staticfiles %}
      <dt><strong><a class="toggleTemplate" href="{{ staticfile.url }}">{{ staticfile }}</a></strong></dt>
      <dd><samp>{{ staticfile.real_path }}</samp></dd>
    {% endfor %}
  </dl>
{% else %}
  <p>{% trans "None" %}</p>
{% endif %}


{% for finder, payload in staticfiles_finders.items %}
  <h4>{{ finder }} ({% blocktrans count payload|length as payload_count %}{{ payload_count }} file{% plural %}{{ payload_count }} files{% endblocktrans %})</h4>
  <table>
    <thead>
      <tr>
        <th>{% trans 'Path' %}</th>
        <th>{% trans 'Location' %}</th>
      </tr>
    </thead>
    <tbody>
      {% for path, real_path in payload %}
        <tr>
          <td>{{ path }}</td>
          <td>{{ real_path }}</td>
        </tr>
      {% endfor %}
    </tbody>
  </table>
{% endfor %}
  1. Static File Updated:
{% load i18n %}

<h4>{% blocktrans count staticfiles_dirs|length as dirs_count %}Static file path{% plural %}Static file paths{% endblocktrans %}</h4>
{% if staticfiles_dirs %}
  <ol>
    {% for prefix, staticfiles_dir in staticfiles_dirs %}
      <li>{{ staticfiles_dir }}{% if prefix %} {% blocktrans %}(prefix {{ prefix }}){% endblocktrans %}{% endif %}</li>
    {% endfor %}
  </ol>
{% else %}
  <p>{% trans "None" %}</p>
{% endif %}

<h4>{% blocktrans count staticfiles_apps|length as apps_count %}Static file app{% plural %}Static file apps{% endblocktrans %}</h4>
{% if staticfiles_apps %}
  <ol>
    {% for static_app in staticfiles_apps %}
      <li>{{ static_app }}</li>
    {% endfor %}
  </ol>
{% else %}
  <p>{% trans "None" %}</p>
{% endif %}

<h4>{% blocktrans count staticfiles|length as staticfiles_count %}Static file{% plural %}Static files{% endblocktrans %}</h4>
{% if staticfiles %}
  <dl>
    {% for staticfile in staticfiles %}
      <dt><strong><a class="toggleTemplate" href="{{ staticfile.url }}">{{ staticfile|stringformat:"s" }}</a></strong></dt>
      <dd><samp>{{ staticfile.real_path }}</samp></dd>
    {% endfor %}
  </dl>
{% else %}
  <p>{% trans "None" %}</p>
{% endif %}


{% for finder, payload in staticfiles_finders.items %}
  <h4>{{ finder }} ({% blocktrans count payload|length as payload_count %}{{ payload_count }} file{% plural %}{{ payload_count }} files{% endblocktrans %})</h4>
  <table>
    <thead>
      <tr>
        <th>{% trans 'Path' %}</th>
        <th>{% trans 'Location' %}</th>
      </tr>
    </thead>
    <tbody>
      {% for path, real_path in payload %}
        <tr>
          <td>{{ path }}</td>
          <td>{{ real_path }}</td>
        </tr>
      {% endfor %}
    </tbody>
  </table>
{% endfor %}

@tim-schilling
Copy link
Contributor

Whoops, I meant I'd like to reproduce this with one of my applications. I'll see if using uvicorn and asgi with the example app reproduces it, but I may need you to create a reproducible example otherwise.

@dougwmorrow
Copy link
Author

Let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants