Business Logic inside Django Views

The first place that business logic normally lives is in Django's views. Views are the default place to starting putting any custom code that you need. For example:

def my_view(request):
    # some custom logic here
    # ...
    return render(request, 'my_template.html', {})

Views generally keep code nice and simple to understand, but you inevitably end up repeating the same code across different views. This is fine for small projects or where you want to keep things really simple. However this is the main downside of having all your logic in your views.

The initial solution to this would be to create plain python functions or classes to hold this shared code and they typically end up being held in one of the other methods we will discuss later in this series.

Tomorrow we will look at Models as the container for business logic.