debugging 调试工具栏未显示

kr98yfug  于 2023-08-06  发布在  其他
关注(0)|答案(2)|浏览(107)
  • 我安装了:pip install django-debug-toolbar
  • 我的设置中有DEBUG=True
  • 我在INSTALLED_APPS中有django.contrib.staticfiles和debug_toolbar
  • 我在STATIC_URL中有STATIC_URL = '/static/'
  • 我在MIDDLEWARE_CLASSES中有'debug_toolbar.middleware.DebugToolbarMiddleware'
  • 我的设置中有INTERNAL_IPS =['127.0.0.1']
  • 我有urlpatterns = [... path('debug/',include(debug_toolbar.urls))]和导入项目URLConfig中的debug_toolbar。
  • 我运行了python manage.py collectstatic,但浏览器中没有显示Debug Toolbar。如何修复?
fzwojiic

fzwojiic1#

是否已在settings.py中包含此设置

settings.py

DEBUG_TOOLBAR_CONFIG = {
    "DISABLE_PANELS": ["debug_toolbar.panels.redirects.RedirectsPanel"],
    "SHOW_TEMPLATE_CONTEXT": True,
    "SHOW_TOOLBAR_CALLBACK": "app_name.small_utils.show_toolbar_callback", # this is the location of the function show_toolbar_callback
}

字符串

app_name/small_utils.py

def show_toolbar_callback(request):
    return not request.is_ajax() and request.user and request.user.is_superuser


在设置这个之后,你的调试工具栏将开始工作。

dy2hfwbg

dy2hfwbg2#

测试它是否在管理区工作。前端的视图模板html代码应该是有效的样板。例如,此页面显示调试栏

<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>HTML 5 Boilerplate</title>
  </head>
  <body>
      <form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
<fieldset>
    <legend><h1>{{ question.question_text }}</h1></legend>
    {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
    {% for choice in question.choice_set.all %}
        <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
        <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
    {% endfor %}
</fieldset>
<input type="submit" value="Vote">
</form>

  </body>
</html>

字符串
但是对于这个html模板,它不起作用:

<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
<fieldset>
    <legend><h1>{{ question.question_text }}</h1></legend>
    {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
    {% for choice in question.choice_set.all %}
        <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
        <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
    {% endfor %}
</fieldset>
<input type="submit" value="Vote">
</form>

相关问题