使用 runserver 命令在 Django 中出现问题

Posted

技术标签:

【中文标题】使用 runserver 命令在 Django 中出现问题【英文标题】:Probkem in Django with runserver command 【发布时间】:2022-01-15 19:30:34 【问题描述】:

我在 django 中启动了一个项目,一切正常,但是我关闭了我的项目,几天后我尝试重新打开,但是当我输入命令“django-admin runserver”时出现此错误:

Traceback (most recent call last):
  File "c:\users\moren\appdata\local\programs\python\python37-32\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\moren\appdata\local\programs\python\python37-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\moren\Envs\myapp\Scripts\django-admin.exe\__main__.py", line 7, in <module>
  File "C:\Users\moren\Envs\myapp\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\moren\Envs\myapp\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\moren\Envs\myapp\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\moren\Envs\myapp\lib\site-packages\django\core\management\commands\runserver.py", line 61, in execute
    super().execute(*args, **options)
  File "C:\Users\moren\Envs\myapp\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\Users\moren\Envs\myapp\lib\site-packages\django\core\management\commands\runserver.py", line 68, in handle
    if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  File "C:\Users\moren\Envs\myapp\lib\site-packages\django\conf\__init__.py", line 82, in __getattr__
    self._setup(name)
  File "C:\Users\moren\Envs\myapp\lib\site-packages\django\conf\__init__.py", line 67, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

当我输入“python manage.py runserver”时,会出现:

c:\users\moren\appdata\local\programs\python\python37-32\python.exe: can't open file 'manage.py': [Errno 2] No such file or directory

如果有人可以帮助我,我将非常感激

【问题讨论】:

可能你忘记激活虚拟环境了? 这能回答你的问题吗? ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings 【参考方案1】:

您需要 cd 进入您的项目并再次运行该命令,所以简而言之,应该在模块“manage.py”所在的位置执行命令python manage.py runserver

【讨论】:

我觉得我做到了,但你能给我举个例子来告诉我如何做到这一点吗? 什么意思?你激活你的虚拟环境了吗?

以上是关于使用 runserver 命令在 Django 中出现问题的主要内容,如果未能解决你的问题,请参考以下文章

如何解决在 Django 中执行 manage.py runserver 命令时的错误原因?

为啥coverage.py 不能正确测量Django 的runserver 命令?

Django manage.py runserver 详细程度

django源码分析——本地runserver分析

python manage.py runserver 没有名为 django.core.management 的模块

python manage.py runserver 127.0.0.1:8000 启动后台有两个启动进程