简体   繁体   English

带有子域配置的django-debug-toolbar面板损坏

[英]Broken django-debug-toolbar panels with sub domains configuration

I'm working on djangoproject.com website with Django Debug Toolbar configured in this dev settings . 我正在djangoproject.com网站上使用在此dev设置中配置的Django Debug Toolbar进行工作。

I founded an issue with that I reported in the issue #796 of djanoproject.com but after some test I think it's only a configuration problem and we need help to solve it. 我在上发现了一个问题 ,该问题在djanoproject.com的问题796中报告,但是经过一些测试,我认为这只是配置问题,我们需要帮助来解决。

All the below sentence are related with the code on branch master used locally. 以下所有句子均与本地使用的分支主机上的代码相关。

Django Debug Toolbar works well for www , for example, if I open http://www.djangoproject.dev:8000/ I can show the toolbar and open the SQL panel. 例如,如果打开http://www.djangoproject.dev:8000/ ,则Django Debug Toolbar可以很好地用于www,我可以显示该工具栏并打开SQL面板。

If I try to open for example http://docs.djangoproject.dev:8000/en/1.11/ I can see the toolbar but I got 0: error if I try to open SQL panel 例如,如果我尝试打开http://docs.djangoproject.dev:8000/zh/1.11/,我可以看到工具栏,但如果尝试打开SQL面板,则会收到0: error

This is the message I saw on browser console: 这是我在浏览器控制台上看到的消息:

Failed to load http://www.djangoproject.dev:8000/ debug /render_panel/?store_id=212b2bb5adc54a3a81b97b6da5547d4c&panel_id=SQLPanel : No 'Access-Control-Allow-Origin' header is present on the requested resource. 无法加载http://www.djangoproject.dev:8000/ debug / render_panel /?store_id = 212b2bb5adc54a3a81b97b6da5547d4c&panel_id = SQLPanel :请求的资源上没有'Access-Control-Allow-Origin'标头。 Origin ' http://docs.djangoproject.dev:8000 ' is therefore not allowed access. 因此,不允许访问源' http://docs.djangoproject.dev:8000 '。

I can see all the data if I open directly the url: 如果直接打开URL,我可以看到所有数据:

http://www.djangoproject.dev:8000/ debug /render_panel/?store_id=212b2bb5adc54a3a81b97b6da5547d4c&panel_id=SQLPanel http://www.djangoproject.dev:8000/ 调试 / render_panel /?store_id = 212b2bb5adc54a3a81b97b6da5547d4c&panel_id = SQLPanel

I think the problem is that the toolbar is trying to open a www. 我认为问题在于工具栏正在尝试打开www。 for the panel instead of a docs. 用于面板而不是文档。 url but I don't know how to update the settings to fix this. 网址,但我不知道如何更新设置以解决此问题。

Can you suggest to us the code to fix this bug and to use panels with in different third-level domains as for docs.djangoproject.com ? 您可以向我们建议用于修复此错误的代码,以及在docs.djangoproject.com的不同第三级域中使用面板吗?

After my PR with the answer of this solution was merged into the djangoproject.com code I want to write the solution so some user can find solution fro similar question. 将解决方案答案的PR合并到djangoproject.com代码后,我想编写解决方案,以便某些用户可以从类似问题中找到解决方案。

This is the answer from @jezdez the issue which explain the problem: 这是来自@jezdez的问题的答案,它解释了问题:

In short: this is a CORS error since it tries to request a resource with Javascript on a different subdomain that isn't allowed to be requested. 简而言之:这是一个CORS错误,因为它尝试在不允许请求的其他子域上使用Javascript请求资源。
The reason why the URL that is rendered by debug_toolbar uses the www subdomain automatically is because the djangoproject project has the django-hosts hosts_override feature installed, which automatically overrides the Django-built-in url template tag witht he one that is capable of resolving Django URLs as a fully qualified URLs and not only as URL paths. 由debug_toolbar呈现的URL自动使用www子域的原因是因为djangoproject项目安装了django-hosts hosts_override功能,该功能会自动覆盖Django内置的url模板标记,并且能够解析Django。 URL是完全合格的URL,而不仅仅是URL路径。
The host_url template tag which is by now also called when the url template tag is used, will fall back to using the host as defined by the DEFAULT_HOST setting, which in our case is www. 现在,使用url模板标记时也会调用host_url模板标记,它将退回到使用DEFAULT_HOST设置所定义的主机(在本例中为www)。 Since we can't easily override the call to the url tag in the debug toolbar template to pass in a different host (eg docs) there is basically only one other option: set the appropriate CORS header Access-Control-Allow-Origin to allow a page loaded under the docs subdomain to access a resource under the www subdomain. 由于我们无法轻易覆盖调试工具栏模板中对url标签的调用以传递不同的主机(例如docs),因此基本上只有一个选项:设置适当的CORS标头Access-Control-Allow-Origin以允许在docs子域下加载的页面,用于访问www子域下的资源。 I would strongly suggest to do that and not mess with the URL generation. 我强烈建议您这样做,并且不要使URL产生混乱。 That said, this is something that should only be applied to the development environment, to reduce the chance for abuse in production. 也就是说,这仅应应用于开发环境,以减少在生产中滥用的机会。
Actually setting the CORS header is as simple as writing a very small middleware that does it manually. 实际上,设置CORS标头就像编写一个很小的手动中间件一样简单。 No need to use a full-blown app like django-cors-headers. 无需使用像django-cors-headers这样的功能强大的应用程序。

I solved the problem with a local-only middelware which set the CORS 'Access-Control-Allow-Origin' header to allow the debug. 我使用仅本地的中间件软件解决了该问题,该软件将CORS的“ Access-Control-Allow-Origin”标头设置为允许调试。

class CORSMiddleware(object):
    """
    Set the CORS 'Access-Control-Allow-Origin' header to allow the debug
    toolbar to work on the docs domain.
    """
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        response['Access-Control-Allow-Origin'] = '*'
        return response

Here is the merged commit that actually is working. 这是实际起作用的合并提交

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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