63

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

Output:

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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Liondancer
  • 15,721
  • 51
  • 149
  • 255

7 Answers7

94

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

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 do you need to do?

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

mkdir ./server/appname # Create a directory from the root level

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

django-admin.py startapp appname ./server/appname
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rahul Gupta
  • 46,769
  • 10
  • 112
  • 126
  • 1
    This is because you are creating your app in a directory where there was one already or simply there were files with same names that are generated by `startapp`. – Ivan Oct 20 '15 at 18:22
  • @Ivan does this mean that everytime I have to remove these files? – Liondancer Oct 20 '15 at 18:34
  • Well, yes. If you have an app in that directory already, what would overwriting achieve? Create it in an empty directory, or, if for some reason you want to overwrite them, just create them somewhere else and copy. – Ivan Oct 20 '15 at 18:39
  • @Liondancer You need to create an `appname` directory first in `/server` and then run the `startapp` command. Check the edit. – Rahul Gupta Oct 20 '15 at 18:44
  • @Ivan Here is the project structure: http://dpaste.com/3YM65DS If I want to create my app within `server` But there are errors because `__init__.py` is already in `server`. `app1` and `app2` have not been created yet. Which is what I am trying to achieve. But I get the file existing errors – Liondancer Oct 20 '15 at 18:49
  • This is because it trying to create the `appname` app inside `/server` instead of `/server/appname`. Now, inside `/server`, there are already app files present so the conflict occurs. Create the `appname` directory inside `/server` and then run `django-admin.py startapp appname ./server/appname` from the project root level. – Rahul Gupta Oct 20 '15 at 18:53
  • @RahulGupta I tried your suggestion but I am getting this error now: http://dpaste.com/15TT9QA – Liondancer Oct 20 '15 at 18:56
  • @Liondancer Check out this Django link to configure the settings. https://docs.djangoproject.com/en/1.8/topics/settings/#designating-the-settings – Rahul Gupta Oct 20 '15 at 18:59
  • 1
    In [Django 2.1] the step 2 will return an error like ' appname conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.' Don't know why. So, I just omit the step 1 and startapp in the Project Root, then move it to the right directory, it works. – C.K. Oct 08 '18 at 22:51
16

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.

cjadeveloper
  • 624
  • 6
  • 6
7

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.

michael_j_ward
  • 4,369
  • 1
  • 24
  • 25
4

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.

Chandan Sharma
  • 2,321
  • 22
  • 22
1

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}}
Eje
  • 354
  • 4
  • 8
thierno
  • 914
  • 11
  • 10
1

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.

New apps created are located in the project root, not inside the project slug #1725

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 the <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>", in your LOCAL_APPS in file config/settings/base.py

Here <project_slug> would be your "server" directory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
O. Altun
  • 685
  • 7
  • 20
0

Use an absolute path for the manage file. It works well for me, and here is an example to run from the destination folder:

python /home/user/project_name/manage.py startupapp app_name
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131