Django:Django 中的堆栈顺序是什么?
我认为这是正确的提问方式。我想知道代码的哪些部分首先执行,第二个执行,等等。
我的假设是,但我不知道:
- 请求
- 中间件
- 视图
- 模型
- 中间件
- 响应
我问的原因是因为我希望在基于请求变量的模型,我正在尝试找到自动将请求添加到模型层而不通过视图传递的最佳方法。我认为某种中间件奇妙的装置将是以某种方式做到这一点的方法。
I think that is the right way to ask it. I'm wondering which parts of the code execute first, second, etc.
My assumption would be, but I don't know:
- Request
- Middleware
- View
- Model
- Middleware
- Response
The reason I'm asking is because I want something to happen dynamicall in the Model based on a request variable and I'm trying to device the best way to automatically add the request in to the model layer without passing in via the views. I would assume that some sort of middleware fantastic contraption would be the way to do it somehow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答你的澄清评论——你无法从这里到达那里。
models.py 只是一个放置模型类的文件,这些类只是可以从任何地方访问的类。除非将
request
对象传递给您正在使用的函数,否则它不存在,并且不存在request.user
。模型可以在任何地方使用,而不仅仅是在有请求
的上下文中。如果您需要使用请求对象,请将其作为参数传递。如果这没有意义,那么你就错误地使用了你的模型。
To answer your clarification comment -- You can't get there from here.
models.py is just a file where you put model classes, which are just classes that get accessed from all over the place. Unless the
request
object is passed to the function you're working with, then it does not exist, and there is norequest.user
. Models can be used from anywhere, not just from within contexts where there's arequest
.If you need to work with the request object, then pass it as a parameter. And if that doesn't make sense, then you're using your model wrong.
模型和模板都不是堆栈的一部分。在视图中完成您的工作。
Neither the model nor the templates are ever part of the stack. Do your work in a view.
我认为它更像是:
I think it's more like: