[Django]-Naming convention for Django URL, templates, models and views

40👍

At first sight there’s no standard, but the usual naming convention is perfectly specified in the wonderful Django Rest Framework, which is almost identical to the style found on the Django tutorial but a little more obvious. If you wanna follow a style, follow that one.

For a Car model the URLs would be (styled as action url):

  • list /cars/
  • create /cars/new/
  • detail /cars/1/
  • update /cars/1/edit/
  • delete /cars/1/delete/
  • any methods not dependent on an object /cars/view-name/
  • any methods dependent on a particular object /cars/1/view-name/

Something you forgot are the URL names (unless this is what you mean with ‘view name’), which would be model-action. e.g. car-list.

The same model_name plus action is used for the Template names (in snake_case) and the View names (in CapitalCase).

Wait, why CapitalCase? Because a much more important standard than naming conventions is to use the powerful class-based views, as opposed to the old method-based views, which lack inheritance and ease of structuring.

If you read the tutorial you’ll notice it begs you to upgrade from method-based to class-based views at around the half-point. Method-based views are useful only for very tiny operations and well, introducing you to Views in the Django Tutorial 😛

TL;DR: Do as you wish (though I applaud your OCD :P) but if there’s something to take away fromm this post is: use class-based views.

Leave a comment