startapp with manage.py to create app in another directory

PythonDjango

Python Problem Overview


My Django project structure is:

/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:

$ ./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.

Python Solutions


Solution 1 - Python

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.

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

So, you can specify the path to your /server/appname directory as the destination value.

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

What you need to do?

1. You need to first create a directory appname inside /server.

mkdir /server/appname # create directory from root level

2. Then, run the startapp command to create the app.

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

Solution 2 - Python

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 Django that is similar to what you want to do. When you want to create a new app, you can use, as the previous answer says,

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.

Solution 3 - Python

If you are already in the server directory, then you can run

python ../manage.py startapp appname

And appname will be created in the server directory instead of in the project root.

Solution 4 - Python

> To Create a New App in Django project.

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.

Solution 5 - Python

The simplest way I found is to go inside the server folder and create an __init__.py file:

touch __init__.py

Then, create any app you want:

django-admin startapp {{APP NAME}}

Solution 6 - Python

If you started the project using cookiecutter-django, then steps are given in the link below.

The difference from the accepted answer and other answers I see here is that you need to change the name of the app in apps.py as an extra step.

https://github.com/pydanny/cookiecutter-django/issues/1725#issuecomment-407493176

To copy paste that here:

1 - create the <name-of-the-app> app with python manage.py startapp
2 - move <name-of-the-app> directory to <project_slug> directory
3 - edit <project_slug>/<name-of-the-app>/apps.py and
change name = "<name-of-the-app>" to name = "<project_slug>.<name-of-the-app>"
4 - add "<project_slug>.<name-of-the-app>.apps.<NameOfTheAppConfigClass>", on your LOCAL_APPS on config/settings/base.py

Here would be your "server" directory.

Solution 7 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLiondancerView Question on Stackoverflow
Solution 1 - PythonRahul GuptaView Answer on Stackoverflow
Solution 2 - PythoncjadeveloperView Answer on Stackoverflow
Solution 3 - Pythonmichael_j_wardView Answer on Stackoverflow
Solution 4 - PythonChandan SharmaView Answer on Stackoverflow
Solution 5 - PythonthiernoView Answer on Stackoverflow
Solution 6 - PythonZargoView Answer on Stackoverflow
Solution 7 - PythonfranciscorodeView Answer on Stackoverflow