我刚刚在本地安装了django调试工具栏来测试我的django应用程序,它运行良好.
我刚刚承诺并推测我的测试远程服务器上的新变化,我看不到工具栏.文件是一样的.
为什么?
我的 Django webapp 允许用户下载动态生成的文本文件:
response = HttpResponse(my_file_contents)
response['Content-Disposition'] = 'attachment; filename="my file.txt"'
return response
Run Code Online (Sandbox Code Playgroud)
我安装了 Django 调试工具栏(0.11.0,因为我无法让 1.0.1 工作),但是当我点击进行下载时,工具栏没有显示有关已下载文件的信息,大概是因为那是一个单独的页面/请求(或者可能因为它是一个非 HTML 文件)。下载的文件也不包含任何调试信息。
如何分析此文件下载的性能?
我已经在 Apache 上部署了 django 站点,并且在我的开发机器上没有出现问题,所以我想打印一些变量来看看发生了什么。
我尝试使用 python 日志记录模块,我这样做了:
import os, logging
FILE = os.getcwd()
logging.basicConfig(filename=os.path.join(FILE,'log.txt'),level=logging.DEBUG)
logging.debug('Write')
Run Code Online (Sandbox Code Playgroud)
在我的开发机器上,我的 django 项目的根目录中可能会显示一个 log.txt。但是,当我执行相同的操作时,我的服务器上的相应目录中没有显示 log.txt 文件。
有谁知道如何调试它?谢谢!
302找到了
位置:/ admin /
Django调试工具栏截获了一个重定向到上面的URL以进行调试查看.您可以单击上面的链接继续正常重定向.
循环介于/admin和之间/admin/login/?next=/admin/
Django 1.10
Django的调试工具栏里== 1.5
新项目(我刚刚安装了Django).试图安装Django调试工具栏.
文档:https://django-debug-toolbar.readthedocs.io/en/stable/installation.html#prerequisites
根据文档,这个版本的django-debug-toolbar与Django 1.10兼容.
你能帮我理解我做错了吗?
settings.py
# { django-debug-toolbar
DEBUG_TOOLBAR_PATCH_SETTINGS = False
INTERNAL_IPS = ['127.0.0.1', ]
if DEBUG:
MIDDLEWARE += ['debug_toolbar.middleware.DebugToolbarMiddleware',]
INSTALLED_APPS += ['debug_toolbar',]
# } django-debug-toolbar
Run Code Online (Sandbox Code Playgroud)
urls.py
from django.conf import settings
from django.conf.urls import include
if settings.DEBUG:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
Run Code Online (Sandbox Code Playgroud)
追溯
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f97d6ab9840>
Traceback (most recent call last):
File "/home/michael/workspace/venv/photoarchive/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/michael/workspace/venv/photoarchive/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line …Run Code Online (Sandbox Code Playgroud) 在尝试使用 进行迁移时python manage.py runserver,我得到了以下回溯:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/infinity/.virtualenvs/project/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/home/infinity/.virtualenvs/project/lib/python3.6/site-packages/django/core/management/__init__.py", line 338, in execute
django.setup()
File "/home/infinity/.virtualenvs/project/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/infinity/.virtualenvs/project/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/infinity/.virtualenvs/project/lib/python3.6/site-packages/django/apps/config.py", line 94, in create
module = import_module(entry)
File "/home/infinity/.virtualenvs/project/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in …Run Code Online (Sandbox Code Playgroud) Django 调试工具栏中的“经过时间”值发生了什么?我假设 CPU 时间是其中的一部分,但还发生了什么?你越明确,就越有帮助!
Resource Value
User CPU time 110.000 msec
System CPU time 50.000 msec
Total CPU time 160.000 msec
Elapsed time 1855.731 msec
Context switches 74 voluntary, 80 involuntary
Run Code Online (Sandbox Code Playgroud)
仅供参考:我已经看到了这一点,但仍然不明白发生了什么。
我正在使用 Postgres 9.3 运行 Django 1.7,运行runserver. 我的数据库中有大约 2 亿行或大约 80GB 的数据。我正在尝试调试为什么相同的查询在 Postgres 中相当快,但在 Django 中很慢。
数据结构是这样的:
class Chemical(models.Model):
code = models.CharField(max_length=9, primary_key=True)
name = models.CharField(max_length=200)
class Prescription(models.Models):
chemical = models.ForeignKey(Chemical)
... other fields
Run Code Online (Sandbox Code Playgroud)
数据库设置了 C 校对规则和合适的索引:
Table "public.frontend_prescription"
Column | Type | Modifiers
id | integer | not null default nextval('frontend_prescription_id_seq'::regclass)
chemical_id | character varying(9) | not null
Indexes:
"frontend_prescription_pkey" PRIMARY KEY, btree (id)
"frontend_prescription_a69d813a" btree (chemical_id)
"frontend_prescription_chemical_id_4619f68f65c49a8_like" btree (chemical_id varchar_pattern_ops)
Run Code Online (Sandbox Code Playgroud)
这是我的看法:
def chemical(request, bnf_code):
c = get_object_or_404(Chemical, …Run Code Online (Sandbox Code Playgroud) 我最近在我的应用程序中安装了debug_toolbar,并报告了查询正在运行两次.
debug_toolbar报告重复的数据库查询.
SET SQL_AUTO_IS_NULL = 0
Duplicated 2 times.
/Users/siquick/Django/soundshelter/soundshelterapp/views.py in release(128)
genre = [release['genre'] for release in context_dict['release']]
Run Code Online (Sandbox Code Playgroud)
views.py的代码段是:
release_list = Releases.objects.filter(id=release_id).values('all_artists','label_no_country','id','title','genre').annotate(cnt=Count('chartsextended'))[:1]
context_dict['release'] = release_list
genre = [release['genre'] for release in context_dict['release']]
label_no_country = [release['label_no_country'] for release in context_dict['release']]
all_artists = [release['all_artists'] for release in context_dict['release']]
title = [release['title'] for release in context_dict['release']]
Run Code Online (Sandbox Code Playgroud)
造成这种重复的原因是什么?我知道它是由这条线造成的,genre = [release['genre'] for release in context_dict['release']]但不确定为什么会发生这种情况.
django ×8
python ×5
apache ×1
django-1.11 ×1
django-admin ×1
performance ×1
postgresql ×1
python-3.6 ×1