Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canonical location for local apps #881

Closed
wiredmind opened this issue Nov 8, 2016 · 16 comments · May be fixed by #3358
Closed

Canonical location for local apps #881

wiredmind opened this issue Nov 8, 2016 · 16 comments · May be fixed by #3358

Comments

@wiredmind
Copy link

Cookiecutter-django defines apps inside project folder, i.e.: red_dwarf/red_dwarf/users. I tried following the convention, but Django complains that it can't find my module, e.g.:

md red_dwarf/posts
python manage.py startapp red_dwarf/posts

config/settings/common.py

LOCAL_APPS = (
    'ref_dwarf.posts.apps.PostsConfig',
)

ImportError: No module named 'posts'

I'd have to adjust sys.path to include the APPS_DIR, but then Django complains, burns, and crashes with Model class [...] doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

So my question is, what is the recommended location for local apps? I've red the docs back to back, gh issues, and the internets but could not find a meaningful answer.

@Sgiath
Copy link

Sgiath commented Nov 25, 2016

As I understand it you should not create your app with startapp command. If you want new app posts just do this in the project root:

mkdir red_dwarf/posts
cd red_dwarf/posts
touch apps.py models.py urls.py views.py

Into your apps.py add this code:

from django.apps import AppConfig


class PostsConfig(AppConfig):
    name = 'red_dwarf.posts'
    verbose_name = 'Posts'

and in config/settings/common.py into LOCAL_APPS add red_dwarf.posts so it will looks like this:

LOCAL_APPS = (
    # custom users app
    'red_dwarf.users.apps.UsersConfig',
    'red_dwarf.posts',
)

And you can start code your app. It works perfectly for me.

@chrisleaman
Copy link

Just extending @Sgiath answer, make sure include __init__.py when creating the other files, otherwise you won't be able to import your app:

touch apps.py models.py urls.py views.py __init__.py

@Sgiath
Copy link

Sgiath commented Nov 29, 2016

Thanks @chrisleaman I forgot that. I'm using PyCharm and it creates this file automatically.

@pydanny
Copy link
Member

pydanny commented Nov 29, 2016

The canonical place is inside the the project folder, otherwise the root folder gets littered with too much stuff. You can use startapp and it should just work. That's why we recommend it. So I partly disagree with @Sgiath's response.

Where I agree with @Sgiath is his mention ensuring that you have apps.py correctly written, which I concur.

In any case, I'll find a place in the docs to put a more detailed description of how to add apps to a Cookiecutter Django project.

@Sgiath
Copy link

Sgiath commented Nov 29, 2016

I fact python manage.py startapp just creates this (https://github.com/django/django/tree/master/django/conf/app_template) structure in whatever path you provide. You can also create your own app template and use it with startapp command (more in the doc ). This is why I think manually create just the files you need is more transparent - python manage.py startapp doesn't do any "magic" in the background.

@umbrae
Copy link

umbrae commented Dec 11, 2016

Just wanted to drop a note that this thread just helped me through the startapp default AppConfig problem through github search. As someone who hasn't touched Django in a few years and with AppConfig being foreign to me, this was super helpful. Thanks for the details on this @pydanny @Sgiath.

@wasabigeek
Copy link
Contributor

@pydanny when you say startapp should just work, do you mean when the apps directory is passed as an argument?

When I used the command without specifying a directory (e.g. python manage.py startapp test):

- project_slug
-- test
-- project_slug

@kodeine
Copy link

kodeine commented Jul 21, 2017

@Sgiath i have a question
why do we use 'red_dwarf.users.apps.UsersConfig', for users app but just 'red_dwarf.posts', without PostsConfig?

@Sgiath
Copy link

Sgiath commented Jul 28, 2017

@kodeine honestly I am not sure 😄 Django can definitely find the config automatically. I guess Users app is just some special case or just preference of the author.
For your apps I recommend using always just equivalent of 'red_dwarf.posts' as described in the Django doc where you can read more about it.

@luzfcb
Copy link
Collaborator

luzfcb commented Jul 28, 2017

by django docs:

New applications should avoid default_app_config. Instead they should require the dotted path to the appropriate AppConfig subclass to be configured explicitly in INSTALLED_APPS.

@japrogramer
Copy link

I was getting an error telling me that the app i was trying to use didn't have an explicit app_label even tho it did, I resolved the issue by instead of doing from .models import Product, I now do Product = apps.get_model('product', 'Product')

@ltankey
Copy link

ltankey commented Jul 3, 2018

Not to jump on an old thread, but no one seemed to address @wasabigeek's comment. If run via docker-compose for someproject...

docker-compose -f local.yml run --rm django python manage.py startapp someapp will create someapp alongside (not within) someproject

The comments here suggest supplying startapp with a directory...

(master)]$ docker-compose -f local.yml run --rm django python manage.py startapp someapp ./someproject/someapp/
Starting someproject_postgres_1 ... done
PostgreSQL is available
CommandError: Destination directory '/app/someproject/someapp' does not exist, please create it first.

Creating it then results in

(master)]$ mkdir someproject/someapp
(master)]$ docker-compose -f local.yml run --rm django python manage.py startapp someapp ./someproject/someapp/
Starting someproject_postgres_1 ... done
PostgreSQL is available
CommandError: 'someapp' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.

which conflicts because I've created a directory within someproject 😕

@browniebroke
Copy link
Member

browniebroke commented Jul 3, 2018

Have you tried the below? I think I recall the path needs to be the parent path where the app will be created...

(master)]$ docker-compose -f local.yml run --rm django python manage.py startapp someapp ./someproject/

https://docs.djangoproject.com/en/2.0/ref/django-admin/#startapp

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

@carlosfunk
Copy link

I've just been struggling with this myself, I had the same issue as @ltankey, tried @browniebroke suggestion but it created the files in the project folder. The only way I could create an app using docker-compose was to log in to a shell and create it from within the project folder.

$ docker-compose -f local.yml run --rm django sh
# cd <project>
# python ../manage.py startapp <appname>

@ltankey
Copy link

ltankey commented Jul 11, 2018

Yikes - that works thanks @carlosfunk

Sucks that's the only way.

@joaodlf
Copy link

joaodlf commented Nov 5, 2018

It's a shame "startapp" does not modify the name in the apps.py file (when pointing the command to other directories besides root).

But that's a Django limitation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.