Opened 21 months ago

Closed 21 months ago

Last modified 13 months ago

#34312 closed New feature (wontfix)

startapp/startproject template should support file renames with the full context

Reported by: Will Gordon Owned by: nobody
Component: Core (Management commands) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (3)

comment:1 by Will Gordon, 21 months ago

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

comment:2 by Mariusz Felisiak, 21 months ago

Resolution: wontfix
Status: newclosed

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 overriding the startapp/startproject commands in your project and making whatever customizations you need.

comment:3 by Greg Sadetsky, 13 months ago

@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 mvs after instantiating a project.

Thank you!

Note: See TracTickets for help on using tickets.
Back to Top