我在哪里可以获得有关Django内部工作原理的技术信息?

时间:2021-02-01 22:04:58

Where can i get the technical manuals/details of how django internals work, i.e. i would like to know when a request comes in from a client;

我在哪里可以获得django内部工作方式的技术手册/细节,即我想知道客户何时收到请求;

  • which django function receives it?
  • 哪个django功能收到它?
  • what middleware get called?
  • 什么中间件被称为?
  • how is the request object created? and what class/function creates it?
  • 请求对象是如何创建的?什么类/功能创建它?
  • What function maps the request to the necessary view?
  • 什么函数将请求映射到必要的视图?
  • How does your code/view get called
  • 您的代码/视图如何被调用

? etc...

?等等...

Paul.G

Paul.G

6 个解决方案

#1


9  

Easiest way to understand the internals of django, is by reading a book specifically written for that.

了解django内部的最简单方法是阅读专门为此编写的书。

Read Pro Django. It provides you a good in depth understanding of the meta programming first and demonstrates how it is used in django models, to create them dynamically.

阅读Pro Django。它首先为您提供了对元编程的深入理解,并演示了如何在django模型中使用它来动态创建它们。

It deals similarly with many other python concepts and how django uses it.

它类似于许多其他python概念以及django如何使用它。

#2


12  

Besides reading the source, here's a few articles I've tagged and bookmarked from a little while ago:

除了阅读源代码之外,这里有一些我刚刚标记过的文章和书签:

I've found James Bennet's blog to be a a great source for information about django workings. His book, Practical Django Projects, is also a must read -- though it isn't focused on internals, you'll still learn about how django works.

我发现James Bennet的博客是django工作信息的重要来源。他的书“实用Django项目”也是必读的 - 尽管它并不专注于内部,你仍然会了解django的工作原理。

#3


10  

"Use the source, Luke." The beauty of open source software is that you can view (and modify) the code yourself.

“使用来源,卢克。”开源软件的优点在于您可以自己查看(和修改)代码。

#4


6  

Simply reading the source might be a bit overwhelming, especially since the upper-most part is a bit confusing (how the webserver hands off the request to Django code). I find a good way to get started reading the code is to set a debugger breakpoint in your view function:

简单地阅读源可能有点压倒性,特别是因为最上面的部分有点令人困惑(web服务器如何将请求移交给Django代码)。我发现开始阅读代码的一个好方法是在视图函数中设置调试器断点:

def time(request):
    import pdb; pdb.set_trace() 
    return HttpResponse(blah blah)

then hit your URL. When the debugger breaks at your breakpoint, examine the stack:

然后点击你的网址。当调试器在断点处中断时,检查堆栈:

(Pdb) where
  c:\abcxyzproject\django\core\management\commands\runserver.py(60)inner_run()
-> run(addr, int(port), handler)
  c:\abcxyzproject\django\core\servers\basehttp.py(698)run()
-> httpd.serve_forever()
  c:\python25\lib\socketserver.py(201)serve_forever()
-> self.handle_request()
  c:\python25\lib\socketserver.py(222)handle_request()
-> self.process_request(request, client_address)
  c:\python25\lib\socketserver.py(241)process_request()
-> self.finish_request(request, client_address)
  c:\python25\lib\socketserver.py(254)finish_request()
-> self.RequestHandlerClass(request, client_address, self)
  c:\abcxyzproject\django\core\servers\basehttp.py(560)__init__()
-> BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
  c:\python25\lib\socketserver.py(522)__init__()
-> self.handle()
  c:\abcxyzproject\django\core\servers\basehttp.py(605)handle()
-> handler.run(self.server.get_app())
  c:\abcxyzproject\django\core\servers\basehttp.py(279)run()
-> self.result = application(self.environ, self.start_response)
  c:\abcxyzproject\django\core\servers\basehttp.py(651)__call__()
-> return self.application(environ, start_response)
  c:\abcxyzproject\django\core\handlers\wsgi.py(241)__call__()
-> response = self.get_response(request)
  c:\abcxyzproject\django\core\handlers\base.py(92)get_response()
-> response = callback(request, *callback_args, **callback_kwargs)
> c:\abcxyzproject\abcxyz\helpers\views.py(118)time()
-> return HttpResponse(
(Pdb)

Now you can see a summary of the path from the deepest part of the web server to your view function. Use the "up" command to move up the stack, and the "list" and "print" command to examine the code and variables at those stack frames.

现在,您可以看到从Web服务器最深部分到视图功能的路径摘要。使用“up”命令向上移动堆栈,使用“list”和“print”命令检查这些堆栈帧的代码和变量。

#5


1  

I doubt there are technical manuals on the subject. It might take a bit of digging, but the API documentation and the source code are your best bets for reliable, up-to-date information.

我怀疑有关于这个问题的技术手册。它可能需要一些挖掘,但API文档和源代码是您获得可靠,最新信息的最佳选择。

#6


0  

The documentation often goes into detail when it has to in order to explain why things work the way they do. One of Django's design goals is to not rely on "magic" as much as possible. However, whenever Django does assume something (template locations within apps, for example) its clearly explained why in the documentation and it always occurs predictably.

文档通常会在必要时进行详细说明,以便解释为什么事情按照他们的方式工作。 Django的设计目标之一是尽可能不依赖“魔法”。但是,每当Django确实假设某些东西(例如应用程序中的模板位置)时,它就会清楚地解释为什么在文档中它总是可预测地发生。

Most of your questions would be answered by implementing a single page.

您可以通过实施单个页面来回答大部分问题。

  1. A request is made from the client for a particular url.
  2. 客户端请求特定网址。
  3. The url resolves what view to call based on the url pattern match.
  4. 该URL根据url模式匹配解析要调用的视图。
  5. The request is passed through the middleware.
  6. 请求通过中间件传递。
  7. The view is called and explicitly passed the request object.
  8. 视图被调用并显式传递请求对象。
  9. The view explicitly calls the template you specify and passes it the context (variables) you specify.
  10. 视图显式调用您指定的模板,并将其指定的上下文(变量)传递给它。
  11. Template context processors, if there are any, then add their own variables to the context.
  12. 模板上下文处理器,如果有,则将自己的变量添加到上下文中。
  13. The context is passed to the template and it is rendered.
  14. 上下文传递给模板并进行渲染。
  15. The rendered template is returned to the client.
  16. 渲染的模板将返回给客户端。

Django Documentation

Django文档

Django Book

Django书

#1


9  

Easiest way to understand the internals of django, is by reading a book specifically written for that.

了解django内部的最简单方法是阅读专门为此编写的书。

Read Pro Django. It provides you a good in depth understanding of the meta programming first and demonstrates how it is used in django models, to create them dynamically.

阅读Pro Django。它首先为您提供了对元编程的深入理解,并演示了如何在django模型中使用它来动态创建它们。

It deals similarly with many other python concepts and how django uses it.

它类似于许多其他python概念以及django如何使用它。

#2


12  

Besides reading the source, here's a few articles I've tagged and bookmarked from a little while ago:

除了阅读源代码之外,这里有一些我刚刚标记过的文章和书签:

I've found James Bennet's blog to be a a great source for information about django workings. His book, Practical Django Projects, is also a must read -- though it isn't focused on internals, you'll still learn about how django works.

我发现James Bennet的博客是django工作信息的重要来源。他的书“实用Django项目”也是必读的 - 尽管它并不专注于内部,你仍然会了解django的工作原理。

#3


10  

"Use the source, Luke." The beauty of open source software is that you can view (and modify) the code yourself.

“使用来源,卢克。”开源软件的优点在于您可以自己查看(和修改)代码。

#4


6  

Simply reading the source might be a bit overwhelming, especially since the upper-most part is a bit confusing (how the webserver hands off the request to Django code). I find a good way to get started reading the code is to set a debugger breakpoint in your view function:

简单地阅读源可能有点压倒性,特别是因为最上面的部分有点令人困惑(web服务器如何将请求移交给Django代码)。我发现开始阅读代码的一个好方法是在视图函数中设置调试器断点:

def time(request):
    import pdb; pdb.set_trace() 
    return HttpResponse(blah blah)

then hit your URL. When the debugger breaks at your breakpoint, examine the stack:

然后点击你的网址。当调试器在断点处中断时,检查堆栈:

(Pdb) where
  c:\abcxyzproject\django\core\management\commands\runserver.py(60)inner_run()
-> run(addr, int(port), handler)
  c:\abcxyzproject\django\core\servers\basehttp.py(698)run()
-> httpd.serve_forever()
  c:\python25\lib\socketserver.py(201)serve_forever()
-> self.handle_request()
  c:\python25\lib\socketserver.py(222)handle_request()
-> self.process_request(request, client_address)
  c:\python25\lib\socketserver.py(241)process_request()
-> self.finish_request(request, client_address)
  c:\python25\lib\socketserver.py(254)finish_request()
-> self.RequestHandlerClass(request, client_address, self)
  c:\abcxyzproject\django\core\servers\basehttp.py(560)__init__()
-> BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
  c:\python25\lib\socketserver.py(522)__init__()
-> self.handle()
  c:\abcxyzproject\django\core\servers\basehttp.py(605)handle()
-> handler.run(self.server.get_app())
  c:\abcxyzproject\django\core\servers\basehttp.py(279)run()
-> self.result = application(self.environ, self.start_response)
  c:\abcxyzproject\django\core\servers\basehttp.py(651)__call__()
-> return self.application(environ, start_response)
  c:\abcxyzproject\django\core\handlers\wsgi.py(241)__call__()
-> response = self.get_response(request)
  c:\abcxyzproject\django\core\handlers\base.py(92)get_response()
-> response = callback(request, *callback_args, **callback_kwargs)
> c:\abcxyzproject\abcxyz\helpers\views.py(118)time()
-> return HttpResponse(
(Pdb)

Now you can see a summary of the path from the deepest part of the web server to your view function. Use the "up" command to move up the stack, and the "list" and "print" command to examine the code and variables at those stack frames.

现在,您可以看到从Web服务器最深部分到视图功能的路径摘要。使用“up”命令向上移动堆栈,使用“list”和“print”命令检查这些堆栈帧的代码和变量。

#5


1  

I doubt there are technical manuals on the subject. It might take a bit of digging, but the API documentation and the source code are your best bets for reliable, up-to-date information.

我怀疑有关于这个问题的技术手册。它可能需要一些挖掘,但API文档和源代码是您获得可靠,最新信息的最佳选择。

#6


0  

The documentation often goes into detail when it has to in order to explain why things work the way they do. One of Django's design goals is to not rely on "magic" as much as possible. However, whenever Django does assume something (template locations within apps, for example) its clearly explained why in the documentation and it always occurs predictably.

文档通常会在必要时进行详细说明,以便解释为什么事情按照他们的方式工作。 Django的设计目标之一是尽可能不依赖“魔法”。但是,每当Django确实假设某些东西(例如应用程序中的模板位置)时,它就会清楚地解释为什么在文档中它总是可预测地发生。

Most of your questions would be answered by implementing a single page.

您可以通过实施单个页面来回答大部分问题。

  1. A request is made from the client for a particular url.
  2. 客户端请求特定网址。
  3. The url resolves what view to call based on the url pattern match.
  4. 该URL根据url模式匹配解析要调用的视图。
  5. The request is passed through the middleware.
  6. 请求通过中间件传递。
  7. The view is called and explicitly passed the request object.
  8. 视图被调用并显式传递请求对象。
  9. The view explicitly calls the template you specify and passes it the context (variables) you specify.
  10. 视图显式调用您指定的模板,并将其指定的上下文(变量)传递给它。
  11. Template context processors, if there are any, then add their own variables to the context.
  12. 模板上下文处理器,如果有,则将自己的变量添加到上下文中。
  13. The context is passed to the template and it is rendered.
  14. 上下文传递给模板并进行渲染。
  15. The rendered template is returned to the client.
  16. 渲染的模板将返回给客户端。

Django Documentation

Django文档

Django Book

Django书