### Start Django Development Server Source: https://github.com/jazzband/django-silk/blob/master/README.md Start the Django development server for the sample project from the root of the project directory. ```bash python manage.py runserver ``` -------------------------------- ### Install Django Silk from a Release Archive Source: https://github.com/jazzband/django-silk/blob/master/docs/quickstart.md Install Silk by downloading a release archive from GitHub and installing it using pip. Replace '' with the specific release version. ```bash pip install django-silk-.tar.gz ``` -------------------------------- ### Install Django Silk from GitHub Tags Source: https://github.com/jazzband/django-silk/blob/master/README.md Install a specific version of django-silk directly from GitHub tags using pip. ```bash pip install git+https://github.com/jazzband/django-silk.git@#egg=django_silk ``` -------------------------------- ### Install Django Silk Directly from GitHub Source: https://github.com/jazzband/django-silk/blob/master/docs/quickstart.md Install Silk directly from the GitHub repository using pip in editable mode. Note that this version may not be stable. ```bash pip install -e git+https://github.com/jazzband/django-silk.git#egg=django_silk ``` -------------------------------- ### Install Django Silk Source: https://github.com/jazzband/django-silk/blob/master/README.md Install the django-silk package using pip. For optional Python snippet formatting, include the [formatting] extra. ```bash pip install django-silk ``` ```bash pip install django-silk[formatting] ``` -------------------------------- ### Install Silk Dependencies for Development Source: https://github.com/jazzband/django-silk/blob/master/README.md Install Silk's development dependencies from the root of the git repository using pip in editable mode. ```bash pip install -e . ``` -------------------------------- ### Install Django Silk with Formatting Extras Source: https://github.com/jazzband/django-silk/blob/master/docs/quickstart.md Install Silk with the 'formatting' extra to enable autopep8 formatting for generated Python snippets. This is optional and enhances the usability of generated code. ```bash pip install django-silk[formatting] ``` -------------------------------- ### Install Django Silk via Pip Source: https://github.com/jazzband/django-silk/blob/master/docs/quickstart.md Install the core Django Silk package using pip. This is the standard method for adding Silk to your Django project. ```bash pip install django-silk ``` -------------------------------- ### Set Database Environment Variables Source: https://github.com/jazzband/django-silk/blob/master/README.md Before running Silk development environment, set the DB_ENGINE and DB_NAME environment variables. Example shown for SQLite. ```bash export DB_ENGINE=sqlite3 export DB_NAME=db.sqlite3 ``` -------------------------------- ### Displaying Silk Profile Data in Templates Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/inclusion/profile_summary.html Use these template tags to render profiling data such as start time, name, path, time taken, and SQL query information. Ensure 'silk_filters' is loaded. ```django {% load silk_filters %} {{ profile.start_time | silk_date_time }} {% if profile.name %}{{ profile.name }}{% elif profile.func_name %}{{ profile.func_name }}{% endif %} {{ profile.path }} {{ profile.time_taken|floatformat:"0" }}ms overall {{ profile.time_spent_on_sql_queries|floatformat:"0" }}ms on queries {{ profile.queries.count }} queries ``` -------------------------------- ### Run Silk Tests Source: https://github.com/jazzband/django-silk/blob/master/README.md Navigate to the 'project' directory and run the tests using the Django management command. ```bash cd project python manage.py test ``` -------------------------------- ### Run Database Migrations for Silk Source: https://github.com/jazzband/django-silk/blob/master/docs/quickstart.md Execute Django's migrate command to create the necessary database tables for Silk. This step is required after adding Silk to your project. ```bash python manage.py migrate ``` -------------------------------- ### Run Django Migrations and Collect Static Files Source: https://github.com/jazzband/django-silk/blob/master/README.md After configuring Silk, run Django's migrate and collectstatic commands to apply database changes and gather static assets. ```bash python manage.py migrate ``` ```bash python manage.py collectstatic ``` -------------------------------- ### Add Silk URLs to Project Source: https://github.com/jazzband/django-silk/blob/master/docs/quickstart.md Include Silk's URL patterns in your project's main urls.py to access the Silk dashboard. This makes the Silk interface available at the '/silk/' path. ```python urlpatterns += [path('silk', include('silk.urls', namespace='silk'))] ``` -------------------------------- ### Configure Query Analysis Parameters Source: https://github.com/jazzband/django-silk/blob/master/docs/configuration.md Pass additional parameters for query profiling when supported by the DBMS, such as 'VERBOSE' or 'FORMAT JSON'. ```python SILKY_EXPLAIN_FLAGS = {'format': 'JSON', 'costs': True} ``` -------------------------------- ### Pass Additional Query Profiling Parameters Source: https://github.com/jazzband/django-silk/blob/master/README.md Configure SILKY_EXPLAIN_FLAGS to pass additional parameters for query profiling when supported by the DBMS, such as 'format' or 'costs'. ```python SILKY_EXPLAIN_FLAGS = {'format':'JSON', 'costs': True} ``` -------------------------------- ### Use Context Manager for Profiling Source: https://github.com/jazzband/django-silk/blob/master/README.md Wrap code blocks with silk_profile to add context to profiling names, useful for identifying performance bottlenecks related to specific data. ```python def post(request, post_id): with silk_profile(name='View Blog Post #%d' % self.pk): p = Post.objects.get(post_id=post_id) return render(request, 'post.html', { 'post': p }) ``` -------------------------------- ### Configure Custom Silk Middleware Source: https://github.com/jazzband/django-silk/blob/master/README.md If using a custom middleware class that subclasses silk.middleware.SilkyMiddleware, specify its path using SILKY_MIDDLEWARE_CLASS and include it in your MIDDLEWARE settings. ```python # Specify the path where is the custom middleware placed SILKY_MIDDLEWARE_CLASS = 'path.to.your.middleware.MyCustomSilkyMiddleware' # Use this variable in list of middleware MIDDLEWARE = [ ... SILKY_MIDDLEWARE_CLASS, ... ] ``` -------------------------------- ### Include Silk URLs in Project URLs Source: https://github.com/jazzband/django-silk/blob/master/README.md Add Silk's URL patterns to your project's main urls.py to enable access to the Silk user interface. ```python urlpatterns += [path('silk/', include('silk.urls', namespace='silk'))] ``` -------------------------------- ### Render Request Summary Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/request.html Renders a summary of the request, typically including status code, method, and path. ```html {% request_summary silk_request %} ``` -------------------------------- ### Enable Meta-Profiling Source: https://github.com/jazzband/django-silk/blob/master/docs/configuration.md Set SILKY_META to True to enable recording the time taken by Silk to save data to the database at the end of each request. This helps in understanding Silk's performance impact. ```python SILKY_META = True ``` -------------------------------- ### Configure Dynamic Profiling for Code Blocks Source: https://github.com/jazzband/django-silk/blob/master/README.md Dynamically profile specific lines within a function by specifying 'start_line' and 'end_line' in the SILKY_DYNAMIC_PROFILING configuration. This allows for granular profiling of code segments. ```python SILKY_DYNAMIC_PROFILING = [{ 'module': 'path.to.module', 'function': 'foo', # Line numbers are relative to the function as opposed to the file in which it resides 'start_line': 1, 'end_line': 2, 'name': 'Slow Foo' }] ``` -------------------------------- ### Display Response Headers Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/request.html Iterates through and displays the response headers, showing both the header name and its value. ```html {% if silk_request.response.headers %} {% heading 'Response Headers' %} {% for k, v in silk_request.response.headers.items %} {{ k.upper }} {{ v }} {% endfor %} {% endif %} ``` -------------------------------- ### Configure Dynamic Method Profiling Source: https://github.com/jazzband/django-silk/blob/master/docs/profiling.md Dynamically profile class methods by specifying the module and the fully qualified method name (e.g., 'MyClass.bar') in `SILKY_DYNAMIC_PROFILING`. ```python SILKY_DYNAMIC_PROFILING = [{ 'module': 'path.to.module', 'function': 'MyClass.bar' }] ``` -------------------------------- ### Configure Django Settings for Silk Source: https://github.com/jazzband/django-silk/blob/master/README.md Add Silk's middleware and context processor to your Django settings. Ensure the middleware is placed correctly in the MIDDLEWARE list. ```python MIDDLEWARE = [ ... 'silk.middleware.SilkyMiddleware', ... ] ``` ```python TEMPLATES = [{ ... 'OPTIONS': { 'context_processors': [ ... 'django.template.context_processors.request', ], }, }] ``` ```python INSTALLED_APPS = ( ... 'silk' ) ``` -------------------------------- ### Display Request Headers Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/request.html Iterates through and displays the request headers, showing both the header name and its value. ```html {% heading 'Request Headers' %} {% for k,v in silk_request.headers.items %} {{ k.upper }} {{ v }} {% endfor %} ``` -------------------------------- ### Enable Authentication and Authorization Source: https://github.com/jazzband/django-silk/blob/master/docs/configuration.md Set these flags to True to require users to log in and have specific permissions to access Silk. By default, only staff users are authorized. ```python SILKY_AUTHENTICATION = True # User must login SILKY_AUTHORISATION = True # User must have permissions ``` -------------------------------- ### Enable Query Analysis Source: https://github.com/jazzband/django-silk/blob/master/docs/configuration.md Set SILKY_ANALYZE_QUERIES to True to enable query analysis for supported DBMS. This provides detailed execution plan information, including actual runtime statistics. ```python SILKY_ANALYZE_QUERIES = True ``` -------------------------------- ### Generate Binary Profiler Files Source: https://github.com/jazzband/django-silk/blob/master/README.md Set SILKY_PYTHON_PROFILER_BINARY to True to also generate a binary .prof file for each request. This file can be used for further analysis with tools like snakeviz. ```python SILKY_PYTHON_PROFILER_BINARY = True ``` -------------------------------- ### Enable Query Analysis Source: https://github.com/jazzband/django-silk/blob/master/README.md Set SILKY_ANALYZE_QUERIES to True to enable query analysis for supported DBMS. Be cautious as this may cause queries to execute twice on some backends like PostgreSQL. ```python SILKY_ANALYZE_QUERIES = True ``` -------------------------------- ### Configure Dynamic Code Block Profiling Source: https://github.com/jazzband/django-silk/blob/master/docs/profiling.md Profile specific lines within a function dynamically by providing `start_line`, `end_line`, and an optional `name` in the `SILKY_DYNAMIC_PROFILING` configuration. ```python SILKY_DYNAMIC_PROFILING = [{ 'module': 'path.to.module', 'function': 'foo', 'start_line': 1, 'end_line': 2, 'name': 'Slow Foo' }] ``` -------------------------------- ### Configure Django Settings for Silk Source: https://github.com/jazzband/django-silk/blob/master/docs/quickstart.md Add Silk's middleware and app configuration to your Django project's settings.py. Ensure 'django.template.context_processors.request' is included in TEMPLATES options. ```python MIDDLEWARE = [ ... 'silk.middleware.SilkyMiddleware', ... ] TEMPLATES = [{ ... 'OPTIONS': { 'context_processors': [ ... 'django.template.context_processors.request', ], }, }] INSTALLED_APPS = [ ... 'silk.apps.SilkAppConfig' ] ``` -------------------------------- ### Configure Dynamic Function Profiling Source: https://github.com/jazzband/django-silk/blob/master/docs/profiling.md Set `SILKY_DYNAMIC_PROFILING` in `settings.py` to dynamically profile specific functions. This configuration targets a module and function name. ```python SILKY_DYNAMIC_PROFILING = [{ 'module': 'path.to.module', 'function': 'foo' }] ``` -------------------------------- ### Profile Code Block with Context Manager Source: https://github.com/jazzband/django-silk/blob/master/docs/profiling.md Utilize `silk_profile` as a context manager to profile specific sections of code within a function. This allows for granular profiling of operations. ```python from silk.profiling.profiler import silk_profile def post(request, post_id): with silk_profile(name='View Blog Post #%d' % self.pk): p = Post.objects.get(pk=post_id) return render(request, 'post.html', { 'post': p }) ``` -------------------------------- ### Display Query Parameters Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/request.html Iterates and displays query parameters if they exist for the request. ```html {% if query_params %} {% heading 'Query Parameters' %} {{ query_params }} {% endif %} ``` -------------------------------- ### Render Request Menu Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/request.html Includes a navigation menu for requests within the Silk interface. ```html {% request_menu request silk_request %} ``` -------------------------------- ### Display Raw Request Body Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/request.html Shows the raw request body if it's not excessively large. If too large, provides a link to view it. ```html {% if silk_request.raw_body %} {% heading 'Raw Request Body' %} {% if silk_request.raw_body|length > 1000 %} The raw request body is **{{ silk_request.raw_body|length }}** characters long and hence is **too big** to show here. Click [here]({% url ) to view the raw request body. {% else %} {{ silk_request.raw_body }} {% endif %} {% endif %} ``` -------------------------------- ### Specify Profiler Result Path Source: https://github.com/jazzband/django-silk/blob/master/README.md Set SILKY_PYTHON_PROFILER_RESULT_PATH to specify a directory for saving profiler results. If not set, MEDIA_ROOT will be used. Ensure the specified directory exists. ```python # If this is not set, MEDIA_ROOT will be used. SILKY_PYTHON_PROFILER_RESULT_PATH = '/path/to/profiles/' ``` -------------------------------- ### Enable Python Profiler Source: https://github.com/jazzband/django-silk/blob/master/README.md Set SILKY_PYTHON_PROFILER to True to enable Python's built-in cProfile profiler for each request. Note that cProfile cannot run concurrently on Python 3.12+. ```python SILKY_PYTHON_PROFILER = True ``` -------------------------------- ### Django Template Tag for Displaying Code Location Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/profile_detail.html Displays the file path and line numbers where a profile was defined. It handles cases where the profile might be defined dynamically. ```html {% if profile.file_path and profile.line_num %} {{ profile.file_path }}:{{ profile.line_num }}{% if profile.end_line_num %}:{{ profile.end_line_num }}{% endif %} {% else %} Location {% endif %} ``` -------------------------------- ### Django Template Tag for Downloading Profile Data Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/profile_detail.html Provides a link to download the raw profile data. This is useful for offline analysis or for use with external profiling tools. ```html {% if silk_request.prof_file %} Click [here]({% url 'silk:request_profile_download' request_id=silk_request.pk %}) to download profile. {% endif %} ``` -------------------------------- ### Display Blind Creation Form Source: https://github.com/jazzband/django-silk/blob/master/project/example_app/templates/example_app/blind_form.html This snippet displays a Django form for creating blind entries. It includes CSRF protection and renders the form fields using `as_p`. ```html {% csrf_token %} {{ form.as_p }} Save ``` -------------------------------- ### Profile Function with Decorator Source: https://github.com/jazzband/django-silk/blob/master/docs/profiling.md Apply the `silk_profile` decorator to a function to profile its execution. Ensure the `name` parameter is descriptive. ```python from silk.profiling.profiler import silk_profile @silk_profile(name='View Blog Post') def post(request, post_id): p = Post.objects.get(pk=post_id) return render(request, 'post.html', { 'post': p }) ``` -------------------------------- ### Run Silk Request Garbage Collection Source: https://github.com/jazzband/django-silk/blob/master/docs/troubleshooting.md Manually trigger Silk's request garbage collection. This is useful to decouple it from web server request processing and avoid deadlocks. Set SILKY_MAX_RECORDED_REQUESTS_CHECK_PERCENT to 0 in settings to enable this. ```bash python manage.py silk_request_garbage_collect ``` -------------------------------- ### Customize Silk Permissions Source: https://github.com/jazzband/django-silk/blob/master/README.md Define a custom function or lambda in settings.py to control which users are authorized to use Silk, overriding the default 'is_staff' check. ```python def my_custom_perms(user): return user.is_allowed_to_use_silk SILKY_PERMISSIONS = my_custom_perms ``` ```python SILKY_PERMISSIONS = lambda user: user.is_superuser ``` -------------------------------- ### Custom Storage for Profiler Results (Django < 4.2) Source: https://github.com/jazzband/django-silk/blob/master/README.md For Django versions older than 4.2 or Django-Silk versions older than 5.1.0, configure custom storage for profiler results using the SILKY_STORAGE_CLASS setting. ```python # For Django < 4.2 or Django-Silk < 5.1.0 SILKY_STORAGE_CLASS = 'path.to.StorageClass' ``` -------------------------------- ### Django Template Tag for Displaying Profiled Code Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/profile_detail.html Renders the actual code snippet that was profiled. It checks if 'code' exists in the context, otherwise displays 'code_error'. ```html {% if code %} {% code code actual_line %} {% elif code_error %} {{ code_error }} {% endif %} ``` -------------------------------- ### Configure Request and Response Body Size Limits Source: https://github.com/jazzband/django-silk/blob/master/docs/configuration.md Control the maximum size of request and response bodies that Silk will save. Set to -1 for no limit on request bodies. Response bodies exceeding the specified size (in KB) will be ignored. ```python SILKY_MAX_REQUEST_BODY_SIZE = -1 # Silk takes anything <0 as no limit SILKY_MAX_RESPONSE_BODY_SIZE = 1024 # If response body>1024kb, ignore ``` -------------------------------- ### Configure Dynamic Profiling for Functions Source: https://github.com/jazzband/django-silk/blob/master/README.md Dynamically apply the silk_profile decorator to functions in specified modules at runtime by configuring SILKY_DYNAMIC_PROFILING in settings.py. This is useful for profiling dependencies or code you cannot directly modify. ```python SILKY_DYNAMIC_PROFILING = [{ 'module': 'path.to.module', 'function': 'MyClass.bar' }] ``` ```python SILKY_DYNAMIC_PROFILING = [{ 'module': 'path.to.module', 'function': 'foo' }] ``` ```python SILKY_DYNAMIC_PROFILING = [{ 'module': 'path.to.module', 'function': 'MyClass.bar' }] ``` -------------------------------- ### Generate Curl Command Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/request.html Provides a 'curl' command to replicate the intercepted request from the command line. Useful for testing and debugging. ```html {% if curl %} {% heading 'Curl' %} Curl is a command-line utility for transferring data from servers. Paste the following into a terminal to repeat this request via command line. {{ curl.strip }} {% endif %} ``` -------------------------------- ### Custom Storage for Profiler Results (Django >= 4.2) Source: https://github.com/jazzband/django-silk/blob/master/README.md For Django versions 4.2 and later, and Django-Silk 5.1.0 and later, configure custom storage for profiler results using the STORAGES setting. Specify the backend class for SILKY_STORAGE. ```python # For Django >= 4.2 and Django-Silk >= 5.1.0: # See https://docs.djangoproject.com/en/5.0/ref/settings/#std-setting-STORAGES STORAGES = { 'SILKY_STORAGE': { 'BACKEND': 'path.to.StorageClass', }, # ... } ``` -------------------------------- ### Configure Max Request and Response Body Size Source: https://github.com/jazzband/django-silk/blob/master/README.md Set SILKY_MAX_REQUEST_BODY_SIZE and SILKY_MAX_RESPONSE_BODY_SIZE in settings.py to control the maximum size of request and response bodies that Silk will save. Use -1 for no limit on request bodies. ```python SILKY_MAX_REQUEST_BODY_SIZE = -1 # Silk takes anything <0 as no limit SILKY_MAX_RESPONSE_BODY_SIZE = 1024 # If response body>1024 bytes, ignore ``` -------------------------------- ### Django Template Tag for Profile Summary Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/profile_detail.html This template tag is used to display a summary of a profile. It requires the 'profile' object to be passed as context. ```html {% profile_summary profile %} ``` -------------------------------- ### Profile Specific Function with Decorator Source: https://github.com/jazzband/django-silk/blob/master/README.md Use the @silk_profile decorator from silk.profiling.profiler to profile specific functions or methods. Provide a name for the profile entry. ```python from silk.profiling.profiler import silk_profile @silk_profile(name='View Blog Post') def post(request, post_id): p = Post.objects.get(pk=post_id) return render(request, 'post.html', { 'post': p }) ``` -------------------------------- ### Display Raw Response Body Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/request.html Shows the decoded raw response body if its length is manageable. For larger bodies, it provides a link to view the full content. ```html {% if silk_request.response.raw_body %} {% heading 'Raw Response Body' %} {% with raw_body=silk_request.response.raw_body_decoded %} {% if raw_body|length > 1000 %} The raw response body is **{{ raw_body|length }}** characters long and hence is **too big** to show here. Click [here]({% url ) to view the raw response body. {% else %} {{ raw_body }} {% endif %} {% endwith %} {% endif %} ``` -------------------------------- ### Profile Method with Decorator Source: https://github.com/jazzband/django-silk/blob/master/docs/profiling.md Use the `silk_profile` decorator on class methods to profile their execution. This is useful for analyzing specific request handlers within a class-based view. ```python from silk.profiling.profiler import silk_profile from django.views import View class MyView(View): @silk_profile(name='View Blog Post') def get(self, request): p = Post.objects.get(pk=post_id) return render(request, 'post.html', { 'post': p }) ``` -------------------------------- ### Record a Fraction of Requests Source: https://github.com/jazzband/django-silk/blob/master/README.md Use SILKY_INTERCEPT_PERCENT to log only a specified percentage of requests on high-load sites. This setting is mutually exclusive with SILKY_INTERCEPT_FUNC. ```python SILKY_INTERCEPT_PERCENT = 50 # log only 50% of requests ``` -------------------------------- ### Custom Logic for Request Recording Source: https://github.com/jazzband/django-silk/blob/master/README.md Define custom logic for intercepting requests by setting SILKY_INTERCEPT_FUNC to a function or lambda. This allows for conditional recording based on request attributes, such as session data. This setting is mutually exclusive with SILKY_INTERCEPT_PERCENT. ```python def my_custom_logic(request): return 'record_requests' in request.session SILKY_INTERCEPT_FUNC = my_custom_logic # log only session has recording enabled. ``` ```python # log only session has recording enabled. SILKY_INTERCEPT_FUNC = lambda request: 'record_requests' in request.session ``` -------------------------------- ### Custom Profiling Logic Function Source: https://github.com/jazzband/django-silk/blob/master/README.md Define a custom function or lambda in settings.py to control when the profiler runs, allowing for conditional profiling based on request attributes like session data. ```python def my_custom_logic(request): return 'profile_requests' in request.session SILKY_PYTHON_PROFILER_FUNC = my_custom_logic # profile only session has recording enabled. ``` ```python # profile only session has recording enabled. SILKY_PYTHON_PROFILER_FUNC = lambda request: 'profile_requests' in request.session ``` -------------------------------- ### Display Processed Response Body Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/request.html Renders the processed response body, usually in JSON format, for readability. If the body exceeds a certain size, a link is provided instead. ```html {% if silk_request.response.body %} This is the body of the HTTP response represented as JSON for easier reading. {% if silk_request.response.body|length > 1000 %} The response body is **{{ silk_request.response.body|length }}** characters long and hence is **too big** to show here. Click [here]({% url ) to view the response body. {% else %} {{ silk_request.response.body }} {% endif %} {% endif %} ``` -------------------------------- ### Django Template Tag for Profile Graph URL Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/profile_detail.html Generates a URL for the profile graph visualization. This URL is then used to fetch the graph data, typically for rendering in a JavaScript-based chart. ```html {% url 'silk:request_profile_dot' request_id=silk_request.pk as profile_dot_url %} {{ profile_dot_url|json_script:'profileDotURL' }} ``` -------------------------------- ### Custom Permissions for Silk Access Source: https://github.com/jazzband/django-silk/blob/master/docs/configuration.md Define a custom function to determine user permissions for accessing Silk. This function should return True if the user is allowed. ```python def my_custom_perms(user): return user.is_allowed_to_use_silk SILKY_PERMISSIONS = my_custom_perms ``` -------------------------------- ### Adjust Request Data Check Percentage Source: https://github.com/jazzband/django-silk/blob/master/docs/configuration.md Control the percentage of requests on which garbage collection for old request/response data is run. This helps reduce overhead. ```python SILKY_MAX_RECORDED_REQUESTS_CHECK_PERCENT = 10 ``` -------------------------------- ### Display Processed Request Body Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/request.html Displays the processed request body, typically as JSON, if it's not too large. Otherwise, offers a link to view the full body. ```html {% if silk_request.body %} {% heading 'Request Body' %} {% if silk_request.body|length > 1000 %} The processed request body is **{{ silk_request.body|length }}** characters long and hence is **too big** to show here. Click [here]({% url ) to view the request body. {% else %} This is the body of the HTTP request represented as JSON for easier reading. {{ silk_request.body }} {% endif %} {% endif %} ``` -------------------------------- ### Iterate and Display Blinds in Django Template Source: https://github.com/jazzband/django-silk/blob/master/project/example_app/templates/example_app/index.html This template code iterates over a collection of 'blinds' and displays their properties. It conditionally renders an image if a photo exists and shows 'Yes' or 'No' based on the 'child_safe' attribute. ```django {% for blind in blinds %} {% endfor %} ``` ```django {% if blind.photo %}![]({{ blind.photo.url }}){% endif %} ``` ```django {{ blind.name }} ``` ```django {% if blind.child_safe %}Yes{% else %}No{% endif %} ``` -------------------------------- ### Extended Profiler File Name Source: https://github.com/jazzband/django-silk/blob/master/README.md Set SILKY_PYTHON_PROFILER_EXTENDED_FILE_NAME to True to include a stub of the request path in the profiler file name, aiding in identifying the endpoint that generated the file. ```python SILKY_PYTHON_PROFILER_EXTENDED_FILE_NAME = True ``` -------------------------------- ### Limit Stored Request/Response Rows Source: https://github.com/jazzband/django-silk/blob/master/docs/configuration.md Configure the maximum number of request/response rows Silk should store to manage disk space. This setting triggers garbage collection for old data. ```python SILKY_MAX_RECORDED_REQUESTS = 10**4 ``` -------------------------------- ### Django Login Form Template Source: https://github.com/jazzband/django-silk/blob/master/project/example_app/templates/example_app/login.html Use this template snippet to render a login form in Django. It includes error display, CSRF token, and fields for username and password. ```html {% if form.errors %} Your username and password didn't match. Please try again. {% endif %} {% csrf_token %} {{ form.username.label_tag }} {{ form.username }} {{ form.password.label_tag }} {{ form.password }} ``` -------------------------------- ### Dynamic Profiling Import Behavior Source: https://github.com/jazzband/django-silk/blob/master/docs/profiling.md When using dynamic profiling, Silk decorates the function within its source module. If the function is imported into another module before dynamic profiling is configured, the profiling will not be applied to the imported function. ```python SILKY_DYNAMIC_PROFILING = [{ 'module': 'my.module', 'function': 'foo' }] ``` -------------------------------- ### Limit Stored Request/Response Data Source: https://github.com/jazzband/django-silk/blob/master/README.md Configure SILKY_MAX_RECORDED_REQUESTS to limit the number of request/response rows stored by Silk, aiding in garbage collection and managing database size. The garbage collection process itself is run on a percentage of requests, adjustable via SILKY_MAX_RECORDED_REQUESTS_CHECK_PERCENT. ```python SILKY_MAX_RECORDED_REQUESTS = 10**4 ``` ```python SILKY_MAX_RECORDED_REQUESTS_CHECK_PERCENT = 10 ``` -------------------------------- ### Clear All Logged Data Source: https://github.com/jazzband/django-silk/blob/master/README.md Use the `silk_clear_request_log` management command to remove all logged request and response data from Silk. ```bash python manage.py silk_clear_request_log ``` -------------------------------- ### Generate Django Test Client Code Source: https://github.com/jazzband/django-silk/blob/master/silk/templates/silk/request.html Generates Python code using the Django test client to replicate the intercepted request. Suitable for use in unit tests. ```html {% if client %} {% heading 'Django Test Client' %} The following is working python code that makes use of the Django test client. It can be used to replicate this request from within a Django unit test, or simply as standalone Python. {{ client.strip }} {% endif %} ``` -------------------------------- ### Custom Sensitive Data Keys Source: https://github.com/jazzband/django-silk/blob/master/README.md Modify SILKY_SENSITIVE_KEYS to define your own set of sensitive keywords for filtering request body data. ```python SILKY_SENSITIVE_KEYS = {'custom-password'} ``` -------------------------------- ### Profile Specific Method in Class with Decorator Source: https://github.com/jazzband/django-silk/blob/master/README.md The @silk_profile decorator can also be applied to methods within class-based views to profile their execution. ```python from silk.profiling.profiler import silk_profile from django.views import View # Profile a method in a view class class MyView(View): @silk_profile(name='View Blog Post') def get(self, request): p = Post.objects.get(pk=post_id) return render(request, 'post.html', { 'post': p }) ``` -------------------------------- ### Default Sensitive Data Keys Source: https://github.com/jazzband/django-silk/blob/master/README.md Silk filters values containing default sensitive keys (case-insensitive) in request bodies. These include 'username', 'api', 'token', 'key', 'secret', 'password', 'signature'. ```python SILKY_SENSITIVE_KEYS = {'username', 'api', 'token', 'key', 'secret', 'password', 'signature'} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.