[英]Disabling Django Silk for specific URLs
I have an API endpoint on which I can upload a photo using a multipart request.我有一个 API 端点,我可以在其上使用多部分请求上传照片。 When Silk is trying to parse the request, I get a decoding error.
当 Silk 尝试解析请求时,出现解码错误。
I now want to disable Silk for certain URL endpoints.我现在想为某些 URL 端点禁用 Silk。 Is this already possible?
这已经可能了吗? If so, how should I configure this?
如果是这样,我应该如何配置它? If not, what is the easiest way of temporarily disabling Silk altogether?
如果没有,暂时完全禁用 Silk 的最简单方法是什么?
Link to Github issue: https://github.com/jazzband/django-silk/issues/292 Github问题链接: https://github.com/jazzband/django-silk/issues/292
It is not possible yet, but you can inherit from the Silk middleware and exclude some views from being silked using process_view django middleware method. 尚不可能 ,但是您可以从Silk中间件继承,并使用process_view django中间件方法排除某些视图。 Eg class-based views in django has
view_class
attribute so you can figure out view class in middleware: 例如,Django中基于类的视图具有
view_class
属性,因此您可以找出中间件中的视图类:
def process_view(request, view_func, view_args, view_kwargs):
if view_func.view_class == SomeClassBasedView:
# ignore it
else:
return super().process_view(request, view_func, view_args, view_kwargs)
The easiest way of temporarily disabling Silk altogether is removing it from middleware list. 暂时完全禁用Silk的最简单方法是将其从中间件列表中删除。
An improvement on milad's solution is to set SILKY_IGNORE_PATHS
to a custom class with a containment method. milad 的解决方案的改进是将
SILKY_IGNORE_PATHS
设置为自定义 class 并使用包含方法。 This allows ignoring urls that include path arguments.这允许忽略包含路径 arguments 的 url。
In urls.py:在 url.py 中:
from silk.config import SilkyConfig
urlpatterns = [
...
]
class PathMatcher:
def __init__(self, url_patterns):
self.url_patterns = url_patterns
def __contains__(self, item):
item = item.lstrip('/')
return any(p.pattern.match(item) for p in self.url_patterns)
SilkyConfig().SILKY_IGNORE_PATHS = PathMatcher(
urlpatterns[5:10] + # a subset of the patterns specified in urlpatterns
[u for u in router.urls if u.name.startswith('analytics-event')] + # a subset of DRF router urls
[re_path('^admin/analytics/event/(.*)', lambda: None)] # hardcoded path
)
我使用 django-silk==4.2.0 并且它具有 SILKY_IGNORE_PATHS 配置,您可以在 django 设置中进行设置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.