Skip to content

Commit

Permalink
Fixed #35591 -- Added unsuitable for production console warning to ru…
Browse files Browse the repository at this point in the history
…nserver.
  • Loading branch information
nanorepublica authored and sarahboyce committed Aug 9, 2024
1 parent 9582745 commit 69aa13f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
9 changes: 9 additions & 0 deletions django/core/management/commands/runserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,12 @@ def on_bind(self, server_port):
f"Quit the server with {quit_command}.",
file=self.stdout,
)
if os.environ.get("HIDE_PRODUCTION_WARNING") != "true":
self.stdout.write(
self.style.WARNING(
"WARNING: This is a development server. Do not use it in a "
"production setting. Use a production WSGI or ASGI server "
"instead.\nFor more information on production servers see: "
"https://docs.djangoproject.com/en/stable/howto/deployment/"
)
)
3 changes: 3 additions & 0 deletions docs/intro/tutorial01.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ You'll see the following output on the command line:
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

WARNING: This is a development server. Do not use it in a production setting. Use a production WSGI or ASGI server instead.
For more information on production servers see: https://docs.djangoproject.com/en/stable/howto/deployment/

.. note::
Ignore the warning about unapplied database migrations for now; we'll deal
with the database shortly.
Expand Down
14 changes: 14 additions & 0 deletions docs/ref/django-admin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,20 @@ multithreaded by default.
Uses IPv6 for the development server. This changes the default IP address from
``127.0.0.1`` to ``::1``.

.. envvar:: HIDE_PRODUCTION_WARNING

.. versionadded:: 5.2

By default, a warning is printed to the console that ``runserver`` is not
suitable for production:

.. code-block:: text

WARNING: This is a development server. Do not use it in a production setting. Use a production WSGI or ASGI server instead.
For more information on production servers see: https://docs.djangoproject.com/en/stable/howto/deployment/

Set this environment variable to ``"true"`` to hide this warning.

Examples of using different ports and addresses
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
5 changes: 4 additions & 1 deletion docs/releases/5.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ Logging
Management Commands
~~~~~~~~~~~~~~~~~~~

* ...
* A new warning is printed to the console when running :djadmin:`runserver` that
``runserver`` is unsuitable for production. This warning can be hidden by
setting the :envvar:`HIDE_PRODUCTION_WARNING` environment variable to
``"true"``.

Migrations
~~~~~~~~~~
Expand Down
31 changes: 31 additions & 0 deletions tests/admin_scripts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,13 @@ def test_zero_ip_addr(self):
"Starting development server at http://0.0.0.0:8000/",
self.output.getvalue(),
)
self.assertIn(
"WARNING: This is a development server. Do not use it in a "
"production setting. Use a production WSGI or ASGI server instead."
"\nFor more information on production servers see: "
"https://docs.djangoproject.com/en/stable/howto/deployment/",
self.output.getvalue(),
)

def test_on_bind(self):
self.cmd.addr = "127.0.0.1"
Expand All @@ -1606,6 +1613,30 @@ def test_on_bind(self):
"Starting development server at http://127.0.0.1:14437/",
self.output.getvalue(),
)
self.assertIn(
"WARNING: This is a development server. Do not use it in a "
"production setting. Use a production WSGI or ASGI server instead."
"\nFor more information on production servers see: "
"https://docs.djangoproject.com/en/stable/howto/deployment/",
self.output.getvalue(),
)

@mock.patch.dict(os.environ, {"HIDE_PRODUCTION_WARNING": "true"})
def test_hide_production_warning_with_environment_variable(self):
self.cmd.addr = "0"
self.cmd._raw_ipv6 = False
self.cmd.on_bind("8000")
self.assertIn(
"Starting development server at http://0.0.0.0:8000/",
self.output.getvalue(),
)
self.assertNotIn(
"WARNING: This is a development server. Do not use it in a "
"production setting. Use a production WSGI or ASGI server instead."
"\nFor more information on production servers see: "
"https://docs.djangoproject.com/en/stable/howto/deployment/",
self.output.getvalue(),
)

@unittest.skipUnless(socket.has_ipv6, "platform doesn't support IPv6")
def test_runner_addrport_ipv6(self):
Expand Down

0 comments on commit 69aa13f

Please sign in to comment.