{"title":"如何使用 Daphne 托管 Django","version":"5.0","locale":"zh-hans","docname":"howto/deployment/asgi/daphne","url":"/zh-hans/5.0/howto/deployment/asgi/daphne/","canonical":"https://djangodocs.dev/zh-hans/5.0/howto/deployment/asgi/daphne/","summary":"Daphne 是一个纯 Python 的 ASGI 服务器，由 Django 项目的成员维护。它充当了 ASGI 的参考服务器。 安装 Daphne Link to this heading # 你可以使用 pip 安装 Daphne： Shell Copy python -m pip install daphne 在…","html":"<h1>如何使用 Daphne 托管 Django<a class=\"heading-anchor\" href=\"#how-to-use-django-with-daphne\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p><a class=\"extlink-pypi reference external\" href=\"https://pypi.org/project/daphne/\">Daphne</a> 是一个纯 Python 的 ASGI 服务器，由 Django 项目的成员维护。它充当了 ASGI 的参考服务器。</p>\n<section id=\"installing-daphne\">\n<h2>安装 Daphne<a class=\"heading-anchor\" href=\"#installing-daphne\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>你可以使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">pip</span></code> 安装 Daphne：</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>python<span class=\"w\"> </span>-m<span class=\"w\"> </span>pip<span class=\"w\"> </span>install<span class=\"w\"> </span>daphne\n</code></pre></div>\n</section>\n<section id=\"running-django-in-daphne\">\n<h2>在 Daphne 中运行 Django<a class=\"heading-anchor\" href=\"#running-django-in-daphne\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>一旦 Daphne 安装完毕，你就可以使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">daphne</span></code> 命令了，它将用来启动 Daphne 服务进程。在最简单的情形下，Daphne 加上包含一个 ASGI 应用模块的位置和应用的名称（以冒号分隔）。</p>\n<p>对于一个典型的 Django 项目，调用 Daphne 的方式如下：</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>daphne<span class=\"w\"> </span>myproject.asgi:application\n</code></pre></div>\n<p>它将开启一个进程，监听 <code class=\"docutils literal notranslate\"><span class=\"pre\">127.0.0.1:8000</span></code>。这需要你的项目位于 Python path 上。为了确保这点，你应该在与 <code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span></code> 文件相同的路径中运行这个命令。</p>\n</section>\n<section id=\"integration-with-runserver\">\n<span id=\"daphne-runserver\"></span><h2>与 <code class=\"docutils literal notranslate\"><span class=\"pre\">runserver</span></code> 集成<a class=\"heading-anchor\" href=\"#integration-with-runserver\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Daphne 提供了一个 <a class=\"reference internal\" href=\"/zh-hans/5.0/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">runserver</span></code></a> 命令，用于在开发过程中使用 ASGI 来提供站点服务。</p>\n<p>这可以通过将 <code class=\"docutils literal notranslate\"><span class=\"pre\">daphne</span></code> 添加到你的 <a class=\"reference internal\" href=\"/zh-hans/5.0/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> 的开头，并添加一个指向你的 ASGI 应用程序对象的 <code class=\"docutils literal notranslate\"><span class=\"pre\">ASGI_APPLICATION</span></code> 设置来启用：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">INSTALLED_APPS</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s2\">&quot;daphne&quot;</span><span class=\"p\">,</span>\n    <span class=\"o\">...</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n\n<span class=\"n\">ASGI_APPLICATION</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;myproject.asgi.application&quot;</span>\n</code></pre></div>\n</section>","rootId":"how-to-use-django-with-daphne","toc":[{"title":"安装 Daphne","anchor":"installing-daphne","children":[]},{"title":"在 Daphne 中运行 Django","anchor":"running-django-in-daphne","children":[]},{"title":"与 runserver 集成","anchor":"integration-with-runserver","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"操作指南","url":"/zh-hans/5.0/howto/"},{"docname":"howto/deployment/index","title":"如何部署 Django","url":"/zh-hans/5.0/howto/deployment/"},{"docname":"howto/deployment/asgi/index","title":"如何使用 ASGI 来部署","url":"/zh-hans/5.0/howto/deployment/asgi/"}],"prev":{"docname":"howto/deployment/asgi/index","title":"如何使用 ASGI 来部署","url":"/zh-hans/5.0/howto/deployment/asgi/"},"next":{"docname":"howto/deployment/asgi/hypercorn","title":"如何使用 Hypercorn 托管 Django。","url":"/zh-hans/5.0/howto/deployment/asgi/hypercorn/"},"formats":{"html":"/zh-hans/5.0/howto/deployment/asgi/daphne/","markdown":"/zh-hans/5.0/howto/deployment/asgi/daphne.md","json":"/zh-hans/5.0/howto/deployment/asgi/daphne.json"},"source":"https://github.com/django/django/blob/stable/5.0.x/docs/howto/deployment/asgi/daphne.txt","official":"https://docs.djangoproject.com/zh-hans/5.0/howto/deployment/asgi/daphne/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}