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.
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.
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
Thanks @chrisleaman I forgot that. I'm using PyCharm and it creates this file automatically.
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.
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.
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.
@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
@Sgiath i have a question
why do we use 'red_dwarf.users.apps.UsersConfig', for users app but just 'red_dwarf.posts', without PostsConfig?
@kodeine honestly I am not sure :smile: 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.
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.
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')
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 馃槙
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
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>
Yikes - that works thanks @carlosfunk
Sucks that's the only way.
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.
Most helpful comment
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.