Is there a command for creating an app using cookiecutter-django?
Asked Answered
F

3

19

Once a Django project has been created using cookiecutter-django, is there a command like python manage.py startapp <app_name> to run instead of writing the new app from scratch?

Feckless answered 3/10, 2016 at 18:40 Comment(0)
B
8

Cookiecutter Django renders a Django project, and included with the files is a manage.py module. If you have Django installed, you can just call python manage.py startapp <app_name> and it should just work.

Borscht answered 17/10, 2016 at 20:56 Comment(3)
For some reason I was thinking that running the usual Django startapp wasn't enough and produced an architecture that doesn't fit with cookiecutter-django's architecture, and because of that there had to be an analog command somewhere... Looks that I was wrong ;)Feckless
I guess that I was thinking something like this: "startapp creates the app in the first level, the same directory where manage.py is. Why not create the app in the <project_name>/ level, where also the users app is?" Where am I wrong?Feckless
Before noticing the answer's author, I started digging around in the Two Scoops to verify this. It actually looks like it's meant to be on the top level. Cookiecutter Django adds a lot of functionality, but still on the same level as startproject. So, while you can add apps like you describe, the first level still remains the typical place.Zacek
R
33

For the sake of completeness, I would like to add that project-specific apps should go into the second level, also when using Cookiecutter Django.

There is a GitHub issue about this, where a project maintainer explains the situation.

What you should do is the following:

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>" to LOCAL_APPS in config/settings/base.py

Rycca answered 13/4, 2020 at 14:26 Comment(1)
Thanks that is very useful. For me, after having run cookiecutter-django, it wasn't even entirely clear if I had to create an app right way, or if it had been somehow handled by the generation process.Borchardt
B
8

Cookiecutter Django renders a Django project, and included with the files is a manage.py module. If you have Django installed, you can just call python manage.py startapp <app_name> and it should just work.

Borscht answered 17/10, 2016 at 20:56 Comment(3)
For some reason I was thinking that running the usual Django startapp wasn't enough and produced an architecture that doesn't fit with cookiecutter-django's architecture, and because of that there had to be an analog command somewhere... Looks that I was wrong ;)Feckless
I guess that I was thinking something like this: "startapp creates the app in the first level, the same directory where manage.py is. Why not create the app in the <project_name>/ level, where also the users app is?" Where am I wrong?Feckless
Before noticing the answer's author, I started digging around in the Two Scoops to verify this. It actually looks like it's meant to be on the top level. Cookiecutter Django adds a lot of functionality, but still on the same level as startproject. So, while you can add apps like you describe, the first level still remains the typical place.Zacek
C
2

This is a good question as it is recommended to create a new app for every feature.Solution to this would be to call manage.py from the project directory. The call will look like this:

python ../manage.py startapp <name_of_app>

You would still need to rename your app.py and you need to add the app settings/base.py under local apps.

Counterproof answered 2/2, 2022 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.