【问题标题】:Django runserver command failedDjango runserver 命令失败
【发布时间】:2021-01-30 19:48:05
【问题描述】:

我已经学习 Python 几个月了,几天前我决定学习其中一个 Web 框架。我在 Django 上找到了一些教程,在我尝试使用“runserver”命令之前,它看起来真的很酷。如您所见,我使用了基本行“python -m django runserver manage.py”并接到了很多电话。我刚刚安装了 Django,并没有更改它的脚本中的任何内容。我只是从创建一个与“python -m django startproject djangoLearn”完美配合的新项目开始。您对如何解决此问题有任何想法吗?这个问题使我无法进一步学习,因为我看不到自己的项目:(( 提前致谢。希望你们都有美好的一天。 我在尝试运行服务器时收到的调用:

D:\Projects\djangoLearn>python -m django runserver manage.py
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main 
    return _run_code(code, main_globals, None,
File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\__main__.py", line 9, in <module>
    management.execute_from_command_line()
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\commands\runserver.py", line 61, in execute
    super().execute(*args, **options)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\base.py", line 371, in execute
    output = self.handle(*args, **options)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\commands\runserver.py", line 68, in handle
    if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\conf\__init__.py", line 82, in __getattr__
    self._setup(name)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\conf\__init__.py", line 63, in _setup
    raise ImproperlyConfigured(
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 -m django runserver manage.py?试着做python manage.py runserver
  • 在设置文件中设置debug 1,像这样,然后试试:DEBUG = int(os.environ.get("DEBUG", default=1))
  • @simon_736,并分享有问题的 manage.py 代码。
  • @Chris 我不知道会这么简单。我一直在使用python -m django runserver manage.py,因为我之前使用过不同的命令,例如python -m django startproject djangoLearn,我认为更改最后两个参数就足够了。感谢您的帮助,祝您有美好的一天:))

标签: python django


【解决方案1】:

我希望你的 virtualenv 被激活了?? 使用它来创建项目django-admin startproject projectname . 请注意它在没有句点的情况下工作的句点。

使用python manage.py startapp appname 创建应用 不要忘记在 INSTALLED_APP 中注册应用程序

我认为您要使用的代码是python manage.py runserver

但如果你使用 python3 使用python3 manage.py runserver,只需在manage.py runserver之前使用你使用的任何东西来启动你的python

【讨论】:

    【解决方案2】:

    您应该首先激活您的虚拟环境。在linux中使用这样的命令:

    source "path-to-activate-file"
    

    在windows中这样的命令:

    \pathto\env\Scripts\activate
    

    【讨论】:

      猜你喜欢
      • 2020-07-08
      • 1970-01-01
      • 2013-01-30
      • 2020-01-30
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      相关资源
      最近更新 更多