{"title":"How to use Django with Daphne","version":"3.0","locale":"zh-hans","docname":"howto/deployment/asgi/daphne","url":"/zh-hans/3.0/howto/deployment/asgi/daphne/","canonical":"https://djangodocs.dev/zh-hans/3.0/howto/deployment/asgi/daphne/","summary":"Daphne is a pure-Python ASGI server for UNIX, maintained by members of the Django project. It acts as the reference server for ASGI. 安装 Daphne Link to this heading…","html":"<h1>How to use Django with Daphne<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=\"reference external\" href=\"https://pypi.org/project/daphne/\">Daphne</a> is a pure-Python ASGI server for UNIX, maintained by members of the\nDjango project. It acts as the reference server for 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=\"bash\"><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=\"bash\"><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>","rootId":"how-to-use-django-with-daphne","toc":[{"title":"安装 Daphne","anchor":"installing-daphne","children":[]},{"title":"在 Daphne 中运行 Django","anchor":"running-django-in-daphne","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"操作指南","url":"/zh-hans/3.0/howto/"},{"docname":"howto/deployment/index","title":"部署 Django","url":"/zh-hans/3.0/howto/deployment/"},{"docname":"howto/deployment/asgi/index","title":"如何使用 ASGI 来部署","url":"/zh-hans/3.0/howto/deployment/asgi/"}],"prev":{"docname":"howto/deployment/asgi/index","title":"如何使用 ASGI 来部署","url":"/zh-hans/3.0/howto/deployment/asgi/"},"next":{"docname":"howto/deployment/asgi/uvicorn","title":"How to use Django with Uvicorn","url":"/zh-hans/3.0/howto/deployment/asgi/uvicorn/"},"formats":{"html":"/zh-hans/3.0/howto/deployment/asgi/daphne/","markdown":"/zh-hans/3.0/howto/deployment/asgi/daphne.md","json":"/zh-hans/3.0/howto/deployment/asgi/daphne.json"},"source":"https://github.com/django/django/blob/stable/3.0.x/docs/howto/deployment/asgi/daphne.txt","official":"https://docs.djangoproject.com/zh-hans/3.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","pt-br","ko","es","el","pl"]}