seddonym
seddonym

Reputation: 17229

Best naming convention for handling multiword Django models?

What is the best naming convention for instance, url and template names of Django models with more than one word?

Instances

  1. yetanothermodel = YetAnotherModel()
  2. yet_another_model = YetAnotherModel()

Url names

  1. 'yetanothermodel_detail'
  2. 'yet_another_model_detail'

Template names

  1. 'yetanothermodel_detail.html'
  2. 'yet_another_model_detail.html'

Upvotes: 3

Views: 2746

Answers (1)

Matt Seymour
Matt Seymour

Reputation: 9395

It is your personal choice. But if you are working in a team I would say you should be applying the Python PEP8 standard of coding; that way all members of the team are using the same process and naming conventions to write their code.

In this instance:

yet_another_model = YetAnotherModel()

Variables names should be lower-case, underscore separated. With class names using the camel casing naming convention.

and

'yet_another_model_detail'

Url names should be treat like a variable name or function name, lower-case separated with _ (underscores).

Templates:

Whilst there is no defined naming convention for templates I treat the naming the same as a function name. So in these cases I go for lower-case with underscore word separation. I also keep the templates inside of the django apps (sometimes, I will have a templates directory which will have a directory for each app name).

I also stick the CRUD type at the end of the filename.

For example:

<app_div>/user_profile.html
<app_dir>/user_profile_update.html
<app_dir>/user_profile_view.html
<app_dir>/user_profile_delete.html

Upvotes: 3

Related Questions