如果已经有 django 项目,如何使用 startproject (django-admin.py)?

发布于 2024-10-13 21:27:26 字数 1472 浏览 1 评论 0原文

我正在尝试启动一个新的 django 项目(使用 django-admin.py ),但不幸的是我总是收到以下错误:

C:\Users\NAME\dev\django>django-admin.py startproject foo
Usage: django-admin.py subcommand [options] [args]
[...]

这同样适用于 django-admin 的任何其他命令。 py - 每个命令都不存在。

我已经有一个 django 项目(在 C:\Users\NAME\dev\django\blog 中),并且我知道如果设置了 DJANGO_SETTINGS_MODULE,startproject 命令将被禁用,但是当我尝试这个时:

>>> import os
>>> os.environ['DJANGO_SETTINGS_MODULE']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\lib\os.py", line 423, in __getitem__
    return self.data[key.upper()]
KeyError: 'DJANGO_SETTINGS_MODULE'

或者更好的是:

>>> from django.conf import settings
>>> dir(settings)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\lib\site-packages\django\utils\functional.py", line 306, in __dir__
    self._setup()
  File "C:\Python26\lib\site-packages\django\conf\__init__.py", line 38, in _set up raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SE TTINGS_MODULE is undefined.

似乎 DJANGO_SETTINGS_MODULE 未定义。有人知道为什么我不能使用 django-admin.py 吗?

Django 1.2.3、W​​indows 7(64 位)

I am trying to start a new django project (using django-admin.py) but unfortunately I'm always getting the following error:

C:\Users\NAME\dev\django>django-admin.py startproject foo
Usage: django-admin.py subcommand [options] [args]
[...]

The same applies to any other command of django-admin.py - every command does not exist.

I already have a django projects (in C:\Users\NAME\dev\django\blog) and I know that the startproject command is disabled if DJANGO_SETTINGS_MODULE is set, but when I try this:

>>> import os
>>> os.environ['DJANGO_SETTINGS_MODULE']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\lib\os.py", line 423, in __getitem__
    return self.data[key.upper()]
KeyError: 'DJANGO_SETTINGS_MODULE'

Or even better this:

>>> from django.conf import settings
>>> dir(settings)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\lib\site-packages\django\utils\functional.py", line 306, in __dir__
    self._setup()
  File "C:\Python26\lib\site-packages\django\conf\__init__.py", line 38, in _set up raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SE TTINGS_MODULE is undefined.

It seems that DJANGO_SETTINGS_MODULE is undefined. Does anyone have an idea why I can't use django-admin.py?

Django 1.2.3, Windows 7 (64bit)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少钕鈤記 2024-10-20 21:27:26

仅当您想要将 django-admin.py 与现有 Django 项目一起使用时,才需要 DJANGO_SETTINGS_MODULE 。
您的问题似乎有所不同,似乎 django-admin.py 由于某种原因不遵守命令行参数。从未在 Windows 上尝试过,但您是否尝试过执行以下操作:

python django-admin.py startproject blog

现在我还看到您说您已经在该文件夹中拥有博客项目。那么为什么需要在同一个文件夹中启动同名的项目呢?请澄清您想要实现的目标。

DJANGO_SETTINGS_MODULE is only needed when you want to use django-admin.py with an existing Django project.
Your issue seems to be different, it seems like django-admin.py does not honor the command line parameters for some reason. Never tried it on Windows, but have you tried doing something like:

python django-admin.py startproject blog

Now I also see you saying you already have blog project in that folder. Why do you need to start project with the same name in the same folder then? Please clarify what you are trying to achieve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文