Improvements to the startproject template

297 views
Skip to first unread message

Tim Allen

unread,
Apr 18, 2022, 1:17:02 PM4/18/22
to Django developers (Contributions to Django itself)
Greetings, friends!

I've issued a PR that makes two changes to the `startproject` template:
  • instead of putting configuration files such as `settings.py`, `wsgi.py`, and the root `urls.py` in `my_project/my_project`, they are created in `my_project/config`
  • start the project with a custom User model app, `users`

Over the years, I've taught or tutored over 100 Djangonauts starting their first project. Having to distinguish between two directories with the same name is a constant pain point in the teaching process - "cd into my_project ... no, the other one!"

It is sometimes better to show rather than tell, so following our own documentation and including a custom User model with the initial project template reinforces the best practice that we explicitly point out in the documentation.

Here's a link to the PR: https://github.com/django/django/pull/15609

My apologies for not starting with a discussion first. I'm an infrequent contributor to the Django codebase!

Regards,

Tim

Ian Foote

unread,
Apr 18, 2022, 4:02:02 PM4/18/22
to django-d...@googlegroups.com
Hi Tim,

This feels like a good idea to me.

Regards,
Ian

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/33cb49d0-2469-47c0-920e-9501245c5a27n%40googlegroups.com.

da...@springbourne-tech.com

unread,
Apr 20, 2022, 7:01:23 AM4/20/22
to Django developers (Contributions to Django itself)
+1 for me - this would be really useful.

John M

unread,
Apr 20, 2022, 10:49:38 AM4/20/22
to django-d...@googlegroups.com

I do exactly this for every new Django project, so it's +1 from me as well.

John

Tom Carrick

unread,
Apr 20, 2022, 12:50:16 PM4/20/22
to django-d...@googlegroups.com
I prefer Adam's suggestion in the forum post as it lets you namespace everything under your project name nicely and avoids package name collisions, although it doesn't solve the problem of having two directories with the same name by default.

That said, either would be an improvement on what we have so I'm in favour of either approach over doing nothing.

Cheers,
Tom

Olivier Dalang

unread,
Apr 20, 2022, 3:22:53 PM4/20/22
to django-d...@googlegroups.com
+1 for Adam's suggestion, I use it as well and like it very much.

> root folder
    - manage.py
    - ...
    > myproject
        - __init__.py
        - settings.py
        - urls.py
        - ...
        > myapp
            - __init__.py
            - models.py
            - ...

Pros:
- everything is well namespaced under myproject (`myproject.settings`, quite straightforward)
- makes it clear that `settings.py`, `urls.py`, etc. concern the project as a whole, and not just an app.
- also nice in settings.INSTALLED_APPS (`myproject.myapp` makes it clear that myapp is part of this project)
- it leaves the root level for stuff like .gitignore, db.sqlite, etc, so the actual source stays clean from such files.
- having a parent package allows (does not require) relative imports across modules of the same project, which more clearly conveys that such apps are tightly coupled
- with manage.py still in the root folder, you don't need to cd into any folder to start working

I use it all the time.

Cheers,

Olivier




Hrushikesh Vaidya

unread,
Apr 20, 2022, 11:01:45 PM4/20/22
to Django developers (Contributions to Django itself)
+1 for the config option with a custom users app

Arthur Pemberton

unread,
Apr 20, 2022, 11:10:06 PM4/20/22
to django-d...@googlegroups.com
For what it's worth, this is the (general) layout I've used for the past 8+ years of my professional Django development.

Arthur

Adrian Torres Justo

unread,
Apr 21, 2022, 5:16:23 AM4/21/22
to Django developers (Contributions to Django itself)
I personally dislike Adam's suggestion and feel like it makes it worse than the current default, but to each their own.

I do prefer the proposed solution of the config directory, I am working on two django projects in parallel and one follows the proposed config scheme and the other doesn't (uses the default) and I find the proposed config scheme more natural to use and navigate, so that's a +1 from me too

Sandro Covo

unread,
Apr 21, 2022, 5:28:45 AM4/21/22
to Django developers (Contributions to Django itself)

As there are different preferences and some see the change as worse than the default and there is already a way to change the template for startproject, wouldn't it be easier to provide different templates and list them in the documentation, so that they can be used with the --template argument? With a short section about each template and the pros and the cons, and for what kind of project one is better suited.

Maybe the three top templates could be shipped directly with django, so that users could choose one with --template=simple, --template=nested or --template=config or something in that direction.

Albert

unread,
Apr 21, 2022, 2:08:49 PM4/21/22
to django-d...@googlegroups.com

It is possible to do in current version of Django with two lines of code:

mkdir my_project

django-admin startproject config my_project

I have been working for many companies that use Django and I have seen that each ot them has their own structure of project.

And usually project is created once per couple of months, so I don't see advantages of changing project structure.

Regards,

Albert

Temat: Re: Improvements to the startproject template

Hazho Human

unread,
Apr 21, 2022, 3:45:43 PM4/21/22
to Django developers (Contributions to Django itself)

Great one, my all support for this

ome chukwuemeka

unread,
Apr 21, 2022, 3:45:43 PM4/21/22
to django-d...@googlegroups.com
I think this is a good suggestion!

Tobias McNulty

unread,
Apr 21, 2022, 3:45:43 PM4/21/22
to django-developers
Tim,

Thanks for taking this on!

Needless to say, I'm all in favor of including a default 'users' app. Adding one (as has long been recommended by the docs) is a task best automated.

I've definitely had the "no the other folder" conversation more than enough times myself. Personally I think I prefer the orderliness of nesting everything under a single Python package that is the project name, but I'm not sure how to do that and solve the "two folders with the same name" problem. In any event, I support whatever the consensus is.

Cheers,
Tobias



--

Ian Foote

unread,
Apr 21, 2022, 6:01:17 PM4/21/22
to django-d...@googlegroups.com
I want to add that I think either proposed change here would be an improvement and that I'd prefer not to see this idea die because of bikeshedding over the best option.

Regards,
Ian

Tim Allen

unread,
Apr 22, 2022, 1:31:03 PM4/22/22
to Django developers (Contributions to Django itself)
Thanks for the excellent feedback, folks. It does sound like we've built a consensus for merging this PR, with other iterative improvements possible later. I like the template idea, but also know that the vast majority of newcomers are going to go with the default.

Once I get the go-ahead from someone with Merger permissions, I'll go ahead and make the changes necessary to the documentation (such as the Tutorial) so we can complete this.

Michael

unread,
Jul 28, 2022, 8:48:56 AM7/28/22
to Django developers (Contributions to Django itself)
Problem with adam's solutions is then where do you place the project's urls.py, asgi.py and wsgi.py? Normally they are in the project dir next to settings.py.
With regards to two dirs with same name, I call the outer dir 'src', which usually has related files/dirs for the project next to it, but are not part of the python source, e.g. requirements.txt

Reply all
Reply to author
Forward
0 new messages