0

I'm trying to layout my Django app, and I just cannot figure out what the appropriate way to do it is. I've read many questions and blog posts, but nothing seems obvious.

Here's my app (I hate the repetition of these terms like buildhealth, but I guess it's idomatic in Django?) - the name of the project, created by django-admin startproject buildhealth and the name of the first app (a performance dashboard) created by django-admin startapp performance is performance):

[ROOT]
├── ...
├── buildhealth
│   ├── __init__.py
│   ├── buildhealth
│   │   ├── __init__.py
│   │   ├── asgi.py
│   │   ├── performance
│   │   │   ├── __init__.py
│   │   │   ├── ...
│   │   │   ├── views.py
│   │   │   └── ...
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   ├── ...
│   ├── manage.py
│   └── staticfiles
│       └── ...
├── poetry.lock
├── pyproject.toml
└── ...

This sort of works, but then i have files all over the place for doing imports which feels terrible. For example - in settings.py, i have to type this:

ROOT_URLCONF = "buildhealth.buildhealth.urls"

This feels super wrong to have two imports like this. Am I laying this out wrong? Am I not importing things correctly? I can't believe that's how it's SUPPOSED to be.

aronchick
  • 6,786
  • 9
  • 48
  • 75

2 Answers2

1

When starting a new project in Django, add a dot at the end of the command to avoid having nested folders with the same name.

django-admin startproject buildhealth .

When doing so, the project folder will be created in the root of the folder you're in when running the command

Joeriksson
  • 26
  • 1
  • 3
0

I haven't used Django for a while, but you can give your settings folder a name different to that of the whole project. I used to name those settings folders _settings with the underscore to keep them at the top in my IDE

You can read this thread for more info: Using a settings file other than settings.py in Django

Ibolit
  • 9,218
  • 7
  • 52
  • 96
  • Thanks! I'll definitely do this - but this kind of nested importing goes on everywhere in the app. Views, urls, etc. – aronchick Jul 31 '22 at 19:17
  • @aronchick Not exactly. With Django projects, it is recommended to have your settings folder (named the same as your project by default) as a settings folder and your root urls folder. All the other stuff goes into "apps", that is, all the actual logic should go into other folders in your root dir. You will not need to import anything from there. As far as importing within the _settings package, you can use relative imports, I personally recommend relative imports within django apps – Ibolit Jul 31 '22 at 20:15
  • You can look at `django-admin start-app` – Ibolit Jul 31 '22 at 20:16
  • sorry, when i say this kind of nested importing goes on everywhere, i just meant that i have to do it all over the place. i'll trying creating a new app like you describe here. – aronchick Jul 31 '22 at 23:17