开发者

Django: What is the stack order in Django?

开发者 https://www.devze.com 2022-12-18 05:52 出处:网络
I think that is the right way to ask it. I\'m wondering which parts of the code execute first, second, etc.

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 kno开发者_高级运维w:

  1. Request
  2. Middleware
  3. View
  4. Model
  5. Middleware
  6. 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.


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 no request.user. Models can be used from anywhere, not just from within contexts where there's a request.

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:

  1. Request
  2. Middleware (URL mapper)
  3. View
    1. Model (if requested by the view)
    2. Template (if requested by the view)
  4. Middleware (response output)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号