Python startapp with manage.py 在另一个目录中创建应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33243661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:05:30  来源:igfitidea点击:

startapp with manage.py to create app in another directory

pythondjango

提问by Liondancer

My Djangoproject structure is:

我的Django项目结构是:

/proj
  /frontend
  /server
    /proj
    /app1
    /app2
  manage.py

How do I run python manage.py startapp app_nameso that my newly created apps are within the /serverdirectory? I tried running django-admin.py startapp appnamewithin the serverdirectory to create the app but I would end up with this error:

如何运行python manage.py startapp app_name以使我新创建的应用程序位于/server目录中?我尝试django-admin.py startapp appnameserver目录中运行以创建应用程序,但最终会出现此错误:

$ ./manage.py runserver
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
    utility.execute()
  File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/core/management/__init__.py", line 343, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/core/management/__init__.py", line 177, in fetch_command
    commands = get_commands()
  File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/core/management/__init__.py", line 72, in get_commands
    for app_config in reversed(list(apps.get_app_configs())):
  File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/apps/registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

采纳答案by Rahul Gupta

You can specify the path to /server/appnamedirectory after appnameas the destinationi.e. where the Django app directory structure will be created.

您可以指定/server/appname目录路径appname作为destinationie 将创建 Django 应用程序目录结构的位置。

From the startappdocs:

startapp文档:

startapp <app_label> [destination] # startapp command usage 

Creates a Django app directory structure for the given app name in the current directory or the given destination.

If only the app name is given, the app directory will be created in the current working directory.

If the optional destination is provided, Django will use that existing directory rather than creating a new one

为当前目录或给定目标中的给定应用程序名称创建 Django 应用程序目录结构

如果只给出了应用程序名称,应用程序目录将在当前工作目录中创建。

如果提供了可选的目的地,Django 将使用现有目录而不是创建一个新目录

So, you can specify the path to your /server/appnamedirectory as the destinationvalue.

因此,您可以将/server/appname目录的路径指定为destination值。

django-admin.py startapp appname [destination] # specify destination

What you need to do?

你需要做什么?

1.You need to first create a directory appnameinside /server.

1.首先需要在appname里面创建一个目录/server

mkdir /server/appname # create directory from root level

2.Then, run the startappcommand to create the app.

2.然后,运行startapp命令创建应用程序。

django-admin.py startapp appname ./server/appname

回答by thierno

The simplest way I found is to go inside the serverfolder and create an __init__.pyfile:

我找到的最简单的方法是进入server文件夹并创建一个__init__.py文件:

touch __init__.py

Then, create any app you want:

然后,创建您想要的任何应用程序:

django-admin startapp {{APP NAME}}

回答by michael_j_ward

If you are already in the serverdirectory, then you can run

如果您已经在server目录中,那么您可以运行

python ../manage.py startapp appname

And appnamewill be created in the serverdirectory instead of in the project root.

并且appname将在server目录中创建,而不是在项目根目录中。

回答by cjadeveloper

I always have my app in an internal folder (the same that Django creates, with the name of the project) following the design of Two Scoops of Djangothat is similar to what you want to do. When you want to create a new app, you can use, as the previous answer says,

我总是将我的应用程序放在一个内部文件夹中(与 Django 创建的文件夹相同,带有项目名称),遵循与您想要做的类似的两个 Django的设计。当你想创建一个新的应用程序时,你可以使用,正如之前的答案所说,

python ../manage.py startapp my_new_app

from within the folder in which you want to create the app. Another thing, even easier that is what I do, is that you can run

从您要在其中创建应用程序的文件夹中。另一件事,更容易的是我所做的,是你可以跑

django-admin startapp my_new_app

from this inner folder, of apps and it will work.

从这个内部文件夹,应用程序,它会工作。

回答by Chandan Sharma

To Create a New App in Django project.

在 Django 项目中创建一个新应用程序。

First:

第一的:

  • Go to your folder using Terminal, where you want to create app
  • 使用终端转到您要在其中创建应用程序的文件夹

Second:

第二:

  • Type the below command in terminal,
  • 在终端中输入以下命令,
django-admin startapp <new_app_name>

Now, you can check, new app created with the given name.

现在,您可以检查使用给定名称创建的新应用程序。

回答by franciscorode

Use absolute path for the manage file work well for me, an example to run from the destination folder

使用绝对路径管理文件对我来说效果很好,一个从目标文件夹运行的例子

python /home/user/project_name/manage.py startupapp app_name