[英]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.