[Django] #34312: startapp/startproject template should support file renames with the full context

9 views
Skip to first unread message

Django

unread,
Feb 5, 2023, 9:41:27 PM2/5/23
to django-...@googlegroups.com
#34312: startapp/startproject template should support file renames with the full
context
-------------------------------------+-------------------------------------
Reporter: Will | Owner: nobody
Gordon |
Type: New | Status: new
feature |
Component: Core | Version: dev
(Management commands) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Currently, startapp/startproject will ''only'' perform a file rename with
"app_name"/"project_name",
https://github.com/django/django/blob/main/django/core/management/templates.py#L180.

I am requesting that it would be possible to use the entirety of values
within the `context`. E.g.


{{{
new_path = os.path.join(top_dir, relative_dir, filename)
# Sort context by length of the key, so that longer keys are tested
first
for key, value in sorted(context.dicts[-1].items(), key=lambda kv:
len(kv[0]), reverse=True):
if key in new_path:
new_path = new_path.replace(key, value)
break
}}}

This would allow the usage of filenames like `camel_case_nameFilename.py-
tpl` to be replaced with `AppNameFilename.py`.

--
Ticket URL: <https://code.djangoproject.com/ticket/34312>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 5, 2023, 9:41:54 PM2/5/23
to django-...@googlegroups.com
#34312: startapp/startproject template should support file renames with the full
context
-------------------------------------+-------------------------------------
Reporter: Will Gordon | Owner: nobody
Type: New feature | Status: new
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Will Gordon):

If there's an agreement, I'm happy to submit a PR for the change.

--
Ticket URL: <https://code.djangoproject.com/ticket/34312#comment:1>

Django

unread,
Feb 5, 2023, 11:34:22 PM2/5/23
to django-...@googlegroups.com
#34312: startapp/startproject template should support file renames with the full
context
-------------------------------------+-------------------------------------
Reporter: Will Gordon | Owner: nobody
Type: New feature | Status: closed

Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => wontfix


Comment:

Thanks for this ticket, however `context` can contain custom variables and
replacing all of them in file name would be backward incompatible and
confusing for users. Moreover, this seems quite niche and sounds like a
good use case for [https://docs.djangoproject.com/en/stable/howto/custom-
management-commands/#overriding-commands overriding] the
`startapp`/`startproject` commands in your project and making whatever
customizations you need.

--
Ticket URL: <https://code.djangoproject.com/ticket/34312#comment:2>

Django

unread,
Sep 30, 2023, 7:00:55 PM9/30/23
to django-...@googlegroups.com
#34312: startapp/startproject template should support file renames with the full
context
-------------------------------------+-------------------------------------
Reporter: Will Gordon | Owner: nobody
Type: New feature | Status: closed
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Greg Sadetsky):

@Will Gordon: for now, you can use use the cookiecutter project
(https://github.com/cookiecutter/cookiecutter) which does support
template-based file renaming

See https://github.com/cookiecutter/cookiecutter-django for example, which
has `{{cookiecutter.project_slug}}` as the directory name.

---

@Mariusz Felisiak: agreed that replacing words in filenames would be
potentially very risky - would you consider template-based replacing, as
above i.e. allowing `{{ project_name }}` to be used & replaced as part of
directory and file names?

Personally speaking, that's all I'd need for my project templates & would
save me from doing hacky `mv`s after instantiating a project.

Thank you!

--
Ticket URL: <https://code.djangoproject.com/ticket/34312#comment:3>

Reply all
Reply to author
Forward
0 new messages