代码之家  ›  专栏  ›  技术社区  ›  jbochi

在Django描绘一个视图的最佳方式是什么?

  •  2
  • jbochi  · 技术社区  · 15 年前

    我已经开发了一个使用django的应用程序,一切都运行良好,但我不知道幕后发生了什么。我想知道:

    • 每个请求命中数据库的次数是多少?
    • 每个查询的执行时间是什么?
    • 渲染模板需要多长时间?
    • 常规分析信息(ncall,每个函数的tottime)。

    有没有中间件可以处理我可以安装的这个问题?哪种最佳实践可以概括我的观点?

    谢谢

    3 回复  |  直到 15 年前
        1
  •  2
  •   Benjamin Wohlwend    15 年前
        2
  •  2
  •   Van Gale    15 年前

    除了分析之外,满足您所有需求的一个项目是优秀的 django debug toolbar .

    对于标准配置文件,需要使用 repoze.profile (这意味着您必须使用wsgi接口运行django,如mod wsgi)。

    如果您是硬核,需要调试内存泄漏,请使用 dozer (也是一个wsgi组件)。

        3
  •  0
  •   Antony Hatchkins Alexander Hamilton    15 年前
    {% if debug %}
        <div id="debug">
        <h2>Queries</h2>
        <p>
            {{ sql_queries|length }} Quer{{ sql_queries|pluralize:"y,ies" }}
            {% ifnotequal sql_queries|length 0 %}
            (<span style="cursor: pointer;" onclick="var s=document.getElementById('debugQueryTable').style;s.display=s.display=='none'?'':'none';this.innerHTML=this.innerHTML=='Show'?'Hide':'Show';">Show</span>)
            {% endifnotequal %}
        </p>
        <table id="debugQueryTable" style="display: none;">
            <col width="1"></col>
            <col></col>
            <col width="1"></col>
            <thead>
            <tr>
            <th scope="col">#</th>
            <th scope="col">SQL</th>
            <th scope="col">Time</th>
            </tr>
            </thead>
            <tbody>
            {% for query in sql_queries %}<tr class="{% cycle odd,even %}">
            <td>{{ forloop.counter }}</td>
            <td>{{ query.sql|escape }}</td>
            <td>{{ query.time }}</td>
            </tr>{% endfor %}
            </tbody>
        </table>
        </div>
    {% endif %}
    

    Django Snippet: Template Query Debug