Home > Software engineering >  DJango project & app layout and Imports seem to be extremely repetative
DJango project & app layout and Imports seem to be extremely repetative

Time:08-01

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.

CodePudding user response:

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

CodePudding user response:

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

  • Related