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
startapp with manage.py to create app in another directory
提问by Liondancer
My Django
project structure is:
我的Django
项目结构是:
/proj
/frontend
/server
/proj
/app1
/app2
manage.py
How do I run python manage.py startapp app_name
so that my newly created apps are within the /server
directory? I tried running django-admin.py startapp appname
within the server
directory to create the app but I would end up with this error:
如何运行python manage.py startapp app_name
以使我新创建的应用程序位于/server
目录中?我尝试django-admin.py startapp appname
在server
目录中运行以创建应用程序,但最终会出现此错误:
$ ./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/appname
directory after appname
as the destination
i.e. where the Django app directory structure will be created.
您可以指定/server/appname
目录路径appname
作为destination
ie 将创建 Django 应用程序目录结构的位置。
From the startapp
docs:
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/appname
directory as the destination
value.
因此,您可以将/server/appname
目录的路径指定为destination
值。
django-admin.py startapp appname [destination] # specify destination
What you need to do?
你需要做什么?
1.You need to first create a directory appname
inside /server
.
1.首先需要在appname
里面创建一个目录/server
。
mkdir /server/appname # create directory from root level
2.Then, run the startapp
command 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 server
folder and create an __init__.py
file:
我找到的最简单的方法是进入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 server
directory, then you can run
如果您已经在server
目录中,那么您可以运行
python ../manage.py startapp appname
And appname
will be created in the server
directory 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