[英]How to setup Django silk profiler
I am completely new to Django and profiling.我对 Django 和分析完全陌生。 I have completed all the steps mentioned in the document for setting up the silk profiler.
我已经完成了文档中提到的设置 Silk Profiler 的所有步骤。 https://github.com/jazzband/silk I did not find any error when I ran the manage.py run server command But when I open the browser and call the necessary api, I don't find anything related to silk.
https://github.com/jazzband/silk我在运行 manage.py run server 命令时没有发现任何错误但是当我打开浏览器并调用必要的 api 时,我没有找到任何与 Silk 相关的内容。 I have no idea where to find the results.
我不知道在哪里可以找到结果。 Any help is greatly appreciated
任何帮助是极大的赞赏
Just went through the beginning with silk.刚开始用丝绸。 As a result:
其结果:
Settings.py:设置.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/'
IN urls.py:在 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')),
]
(Assumimg you are on linux)Now run: (假设你在 linux 上)现在运行:
python manage.py makemigrations;
python manage.py migrate;
python manage.py collectstatic;
then run the server and go to然后运行服务器并转到
127.0.0.1:8000/silk/ 127.0.0.1:8000/丝/
url网址
Django SILK 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
Profilling填充
Decorator applied to views.应用于视图的装饰器。
Function based:基于 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 based:基于 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
})
Context Manager上下文管理器
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
})
And和
python manage.py makemigrations;
python manage.py migrate;
python manage.py collectstatic;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.