Django项目初始设置问题

4 投票
3 回答
25284 浏览
提问于 2025-04-19 19:20

我正在通过官方文档学习Django,在这个教程的过程中,网址是 https://docs.djangoproject.com/en/1.7/intro/tutorial01/,我在创建项目的部分遇到了问题。

当我运行 django-admin.py startproject mysite 时,出现了以下错误:

C:\Python34>django-admin.py startproject mysite
Usage: django-admin.py subcommand [options] [args]
Options:
  -v VERBOSITY, --verbosity=VERBOSITY
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings=SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath=PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Raise on exception
  --no-color            Don't colorize the command output.
  --version             show program's version number and exit
  -h, --help            show this help message and exit
Type 'django-admin.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    migrate
    runfcgi
    runserver
    shell
    sql
    sqlall
    sqlclear
    sqlcustom
    sqldropindexes
    sqlflush
    sqlindexes
    sqlinitialdata
    sqlmigrate
    sqlsequencereset
    squashmigrations
    startapp
    startproject
    syncdb
    test
    testserver
    validate

请注意,由于设置没有正确配置,所以这里只列出了Django的核心命令。

error: Requested setting INSTALLED_APPS, but settings are not configured
    . You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).

我使用的是Python 3.4.1和Django 1.7。我没有安装其他版本的Django,这也是我创建的第一个项目。

3 个回答

1

我在安装Django 2.0.5的时候也遇到了同样的问题。这个问题可以通过使用虚拟环境来解决。


环境信息:

  • Python版本:3.6
  • 操作系统:Ubuntu 18.xx.x

步骤:

  1. 安装虚拟环境 pip install virtualenv
  2. 创建一个虚拟工作空间(叫做'VEnv') virtualenv --python=python3 VEnv
  3. 激活虚拟环境: cd VEnv/ source bin/activate
  4. 安装Django(版本 - 2.0.5) pip install django==2.0.5
  5. 创建项目(项目名称:HelloDot) django-admin startproject HelloDot
  6. 运行服务器,然后可以通过这个链接访问它:"http://127.0.0.1:8000/" cd HelloDot/ python manage.py runserver 8000

想了解更多细节,可以查看这个链接: https://www.howtoforge.com/tutorial/how-to-install-django-on-ubuntu/

25

你只需要运行 django-admin startproject mysite(注意:不是 django-admin.py),因为如果你通过 pip 安装了 Django,系统会在 'C:\Python34\Scripts' 目录下添加一个叫 'django-admin.exe' 的可执行程序。这个目录通常已经在你的环境变量 'PATH' 里了(如果没有,就需要把它加进去)。

0

确保你查看一下故障排除指南,因为看起来你的系统路径中没有正确设置django-admin.py。根据文档:

如果你是通过python setup.py安装Django的,django-admin.py应该在你的系统路径中。如果它不在你的路径里,你可以在site-packages/django/bin找到它,其中site-packages是你Python安装目录下的一个文件夹。你可以考虑从你的路径中的某个地方,比如/usr/local/bin,创建一个指向django-admin.py的符号链接。

你还应该为每个项目使用virtualenv,这样可以让每个项目的依赖关系相互隔离,更容易管理。virtualenvwrapper是一个很有用的工具,可以帮助你创建和管理你的virtualenv。

撰写回答