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?

dolma33
  • 4,133
  • 6
  • 28
  • 48

3 Answers3

28

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

Thomas Kainrad
  • 2,542
  • 21
  • 26
  • 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. – cjauvin Aug 28 '20 at 17:42
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.

pydanny
  • 7,954
  • 6
  • 34
  • 42
  • 1
    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 ;) – dolma33 Oct 24 '16 at 15:39
  • 2
    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 `/` level, where also the `users` app is?" Where am I wrong? – dolma33 Oct 24 '16 at 15:54
  • 3
    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. – Masa May 31 '17 at 21:57
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.

Ron maraz
  • 21
  • 3