<small id='NJDXL'></small><noframes id='NJDXL'>

      <bdo id='NJDXL'></bdo><ul id='NJDXL'></ul>
    <legend id='NJDXL'><style id='NJDXL'><dir id='NJDXL'><q id='NJDXL'></q></dir></style></legend>
    <i id='NJDXL'><tr id='NJDXL'><dt id='NJDXL'><q id='NJDXL'><span id='NJDXL'><b id='NJDXL'><form id='NJDXL'><ins id='NJDXL'></ins><ul id='NJDXL'></ul><sub id='NJDXL'></sub></form><legend id='NJDXL'></legend><bdo id='NJDXL'><pre id='NJDXL'><center id='NJDXL'></center></pre></bdo></b><th id='NJDXL'></th></span></q></dt></tr></i><div id='NJDXL'><tfoot id='NJDXL'></tfoot><dl id='NJDXL'><fieldset id='NJDXL'></fieldset></dl></div>

        <tfoot id='NJDXL'></tfoot>
      1. 仅在 Django 启动 ONCE 时执行代码?

        Execute code when Django starts ONCE only?(仅在 Django 启动 ONCE 时执行代码?)
          <tbody id='TY37x'></tbody>

          <bdo id='TY37x'></bdo><ul id='TY37x'></ul>
          1. <i id='TY37x'><tr id='TY37x'><dt id='TY37x'><q id='TY37x'><span id='TY37x'><b id='TY37x'><form id='TY37x'><ins id='TY37x'></ins><ul id='TY37x'></ul><sub id='TY37x'></sub></form><legend id='TY37x'></legend><bdo id='TY37x'><pre id='TY37x'><center id='TY37x'></center></pre></bdo></b><th id='TY37x'></th></span></q></dt></tr></i><div id='TY37x'><tfoot id='TY37x'></tfoot><dl id='TY37x'><fieldset id='TY37x'></fieldset></dl></div>
          2. <legend id='TY37x'><style id='TY37x'><dir id='TY37x'><q id='TY37x'></q></dir></style></legend>

            • <tfoot id='TY37x'></tfoot>

              <small id='TY37x'></small><noframes id='TY37x'>

                1. 本文介绍了仅在 Django 启动 ONCE 时执行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一个 Django 中间件类,我只想在启动时执行一次,以初始化一些其他任意代码.我遵循了 sdolan 这里,但是Hello"消息输出到终端两次.例如

                  I'm writing a Django Middleware class that I want to execute only once at startup, to initialise some other arbritary code. I've followed the very nice solution posted by sdolan here, but the "Hello" message is output to the terminal twice. E.g.

                  from django.core.exceptions import MiddlewareNotUsed
                  from django.conf import settings
                  
                  class StartupMiddleware(object):
                      def __init__(self):
                          print "Hello world"
                          raise MiddlewareNotUsed('Startup complete')
                  

                  在我的 Django 设置文件中,我将类包含在 MIDDLEWARE_CLASSES 列表中.

                  and in my Django settings file, I've got the class included in the MIDDLEWARE_CLASSES list.

                  但是当我使用 runserver 运行 Django 并请求页面时,我进入了终端

                  But when I run Django using runserver and request a page, I get in the terminal

                  Django version 1.3, using settings 'config.server'
                  Development server is running at http://127.0.0.1:8000/
                  Quit the server with CONTROL-C.
                  Hello world
                  [22/Jul/2011 15:54:36] "GET / HTTP/1.1" 200 698
                  Hello world
                  [22/Jul/2011 15:54:36] "GET /static/css/base.css HTTP/1.1" 200 0
                  

                  任何想法为什么Hello world"被打印两次?谢谢.

                  Any ideas why "Hello world" is printed twice? Thanks.

                  推荐答案

                  更新 Pykler 的回答如下:Django 1.7 现在有一个 挂钩

                  Update from Pykler's answer below: Django 1.7 now has a hook for this

                  不要这样做.

                  您不希望一次性启动的中间件".

                  You don't want "middleware" for a one-time startup thing.

                  您想在顶级 urls.py 中执行代码.该模块被导入并执行一次.

                  You want to execute code in the top-level urls.py. That module is imported and executed once.

                  urls.py

                  from django.confs.urls.defaults import *
                  from my_app import one_time_startup
                  
                  urlpatterns = ...
                  
                  one_time_startup()
                  

                  这篇关于仅在 Django 启动 ONCE 时执行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                      <tbody id='C5j0Y'></tbody>
                    <legend id='C5j0Y'><style id='C5j0Y'><dir id='C5j0Y'><q id='C5j0Y'></q></dir></style></legend>
                  • <tfoot id='C5j0Y'></tfoot>

                        <i id='C5j0Y'><tr id='C5j0Y'><dt id='C5j0Y'><q id='C5j0Y'><span id='C5j0Y'><b id='C5j0Y'><form id='C5j0Y'><ins id='C5j0Y'></ins><ul id='C5j0Y'></ul><sub id='C5j0Y'></sub></form><legend id='C5j0Y'></legend><bdo id='C5j0Y'><pre id='C5j0Y'><center id='C5j0Y'></center></pre></bdo></b><th id='C5j0Y'></th></span></q></dt></tr></i><div id='C5j0Y'><tfoot id='C5j0Y'></tfoot><dl id='C5j0Y'><fieldset id='C5j0Y'></fieldset></dl></div>
                      • <small id='C5j0Y'></small><noframes id='C5j0Y'>

                        • <bdo id='C5j0Y'></bdo><ul id='C5j0Y'></ul>