服务器启动与请求进入时执行什么操作? [英] What gets executed as the server starts vs. as a request comes in?

查看:52
本文介绍了服务器启动与请求进入时执行什么操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在用Django进行类黑客攻击.然后我从settings.py调用我的更改,因为我认为它先于其他运行.

I've been doing some class hacking in Django. And I call my changes from settings.py as I figure this runs before anything else.

但是,我遇到了这种情况对我不起作用.那么,在 settings.py之后是否还有另一个地方,我可以保证在服务器开始处理任何请求之前,它将始终由服务器运行?

However, I've just come across a situation where this doesn't work for me. So, is there another place after settings.py, which I can guarantee will always be run by the server before it starts handling any requests?

推荐答案

永远不要将需要从Django的任何部分导入任何内容的代码放入 settings.py 中.由于Django的许多部分都要求设置可用,因此很可能使您陷入循环导入问题.

You should never put code in settings.py that requires importing anything from any part of Django. Since many parts of Django require settings to be available, this is very likely to get you into circular import problems.

您的 ROOT_URLCONF ( urls.py )是放置要在每个服务器Python进程中运行一次的项目级代码的合理位置,然后再处理任何请求

Your ROOT_URLCONF (urls.py) is a reasonable place to put project-level code that you want run once for each server Python process, before any requests are served.

如果代码是特定于特定应用程序的(只有在使用该应用程序时才需要),则可以将其放入该应用程序的 models.py __ init __.py .

If the code is specific to a particular app (and only needed if that app is in use) then you could put it in that app's models.py or __init__.py.

要更广泛地了解此问题,请参见此博客文章.

For a broader look at the issue, see this blog post.

这篇关于服务器启动与请求进入时执行什么操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆