简体   繁体   English

Django:我如何找到填充特定模板的方法/函数

[英]Django: How can I find methods/functions filling in the specific template

Say, I find some bug in a web interface. 说,我在网络界面中发现了一些错误。 I open firebug and discover element and class, id of this element. 我打开firebug并发现元素和类,这个元素的id。 By them I can then identify a template which contains variables, tags and so on. 通过它们,我可以识别包含变量,标签等的模板。 How can I move forward and reveal in which .py files these variables are filled in? 如何向前移动并显示填充这些变量的.py文件?

I know how it works in Lift framework: when you've found a template there are elements with attributes bound to snippets. 我知道它在Lift框架中是如何工作的:当你找到一个模板时,有些元素的属性绑定到片段。 So you can easily proceed to specific snippets and edit code. 因此,您可以轻松地继续使用特定的代码段并编辑代码。

How does it work in django? 它在django中如何运作? May be, I suppose wrong process... then point me to the right algorithm, please. 可能是,我想错误的过程......然后请指出正确的算法。

In well designed django, you should only have to edit the template. 在精心设计的django中,您只需编辑模板即可。 Good design provides clean separation. 良好的设计提供清洁分离。 It's possible the developer may have been forced todo something unusual...but you could try to edit the template and see what happens (make backup 1st) 有可能开发人员可能被迫做了一些不寻常的事情......但你可以尝试编辑模板并看看会发生什么(使备份成为第1个)

Determining template variable resolution is all about Context . 确定模板变量分辨率完全是关于Context

  1. Use the URL to identify the view being invoked. 使用URL标识要调用的view
  2. Look at the view's return and note a) the template being used, and b) any values being passed in the Context used when the template is being rendered. 查看视图的返回并注意a)正在使用的模板,以及b)在呈现模板时使用的Context中传递的任何值。
  3. Look at settings.py for the list of TEMPLATE_CONTEXT_PROCESSORS . 查看settings.py以获取TEMPLATE_CONTEXT_PROCESSORS列表。 These are routines that are called automatically and invisibly to add values to the Context being passed to the template. 这些是自动且无形地调用的例程,用于向传递给模板的Context添加值。 This is sort of a Man Behind the Curtain™ process that can really trip you up if you don't know about it. 这有点像Curtain™工艺背后的人,如果你不知道它,它真的会让你失望。
  4. Check to see if there are any magic template tags being called (either in the template in question, in a template it extends, or in a template that includes the template) that might be modifying the Context. 检查是否有任何魔术模板标签被调用(在相关模板中,在扩展的模板中,或在包含模板的模板中),可能正在修改上下文。 Sometimes I need use an old-school django snippet called {%expr%} that can do evaluation in the template , but I always use it as close to the point of need as possible to highlight the fact it is being used. 有时我需要使用一个名为{%expr%}的老式django片段,它可以在模板中进行评估,但我总是尽可能地使用它,以突出显示它正在被使用的事实。

Note that because of the way Django template variables are resolved, {{foo.something}} could be either a value or a callable method. 请注意,由于Django模板变量的解析方式, {{foo.something}}可以是值可调用方法。 I have serious issues with this syntax, but that's the way they wrote it. 我对这种语法有严重的问题,但这就是他们编写它的方式。

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

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