繁体   English   中英

如何设置 Django 丝绸轮廓仪

[英]How to setup Django silk profiler

我对 Django 和分析完全陌生。 我已经完成了文档中提到的设置 Silk Profiler 的所有步骤。 https://github.com/jazzband/silk我在运行 manage.py run server 命令时没有发现任何错误但是当我打开浏览器并调用必要的 api 时,我没有找到任何与 Silk 相关的内容。 我不知道在哪里可以找到结果。 任何帮助是极大的赞赏

刚开始用丝绸。 其结果:

设置.py:

DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'silk',
]

MIDDLEWARE = [
    'silk.middleware.SilkyMiddleware',


    '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',
]




# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/


STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_in_env")

#before you change this make sure to create a folder "static" in project directory, otherwise it will throw an error.

STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static'),
        ]



STATIC_URL = '/static/'

在 urls.py 中:

from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^silk/', include('silk.urls', namespace='silk')),
]

(假设你在 linux 上)现在运行:

python manage.py makemigrations;

python manage.py migrate;

python manage.py collectstatic;

然后运行服务器并转到

127.0.0.1:8000/丝/

网址

Django 丝绸

pip install django-silk

MIDDLEWARE_CLASSES = (
    ...
    'silk.middleware.SilkyMiddleware',
    ...
)

INSTALLED_APPS = (
    ...
    'silk'
)

urlpatterns += patterns('', url(r'^silk', include('silk.urls', namespace='silk')))

python manage.py syncdb

填充

应用于视图的装饰器。

基于 Function:

@silk_profile(name='View Blog Post')
def post(request, post_id):
    p = Post.objects.get(pk=post_id)
    return render_to_response('post.html', {
        'post': p
    })

基于 Class:

class MyView(View):
        @silk_profile(name='View Blog Post')
        def get(self, request):
                p = Post.objects.get(pk=post_id)
        return render_to_response('post.html', {
                'post': p
        })

上下文管理器

def post(request, post_id):
    with silk_profile(name='View Blog Post #%d' % self.pk):
        p = Post.objects.get(pk=post_id)
        return render_to_response('post.html', {
                'post': p
        })

python manage.py makemigrations;

python manage.py migrate;

python manage.py collectstatic;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM