简体   繁体   中英

How to setup Django silk profiler

I am completely new to Django and profiling. I have completed all the steps mentioned in the document for setting up the 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. 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:

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:

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:

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/

url

Django SILK

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:

@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 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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