DEV Community

Michael Jones
Michael Jones

Posted on • Updated on

Django to Phoenix - Part 0: Background

Hey there, I wrote a website! I was pretty new to web development and I did my best. I wrote it in Django & Python as I was already familiar with Python, and Django is a friendly framework with a good ecosystem. It was a great experience. As a beginner, I was happy to have Django there to hold my hand along the way.

However time has passed and I am getting into functional programming. Whilst functional techniques are possible in Python, it doesn't feel like a good fit. I've also been reading and listening to people talk about Phoenix, Elixir & the Erlang Virtual Machine and the combination sounds kind of great.

So, I'm going to attempt to move at least some of my website, Tango Timetable, from Django to Phoenix.

Starting Point

The site is entirely in Django, with a couple of cron jobs for weekly emails. It runs using Gunicorn behind Nginx in a Docker container. I use Docker to simplify deployments as I was finding a 'git-pull & restart' approach to be too fragile.

Disclaimer

I am comfortable with Python, experienced-but-no-expert in Django and a beginner at Phoenix & Elixir. I'm going to try to document my efforts here in future posts to share my findings but also to hopefully get guidance from others who might know better.

Latest comments (3)

Collapse
 
sanjay555 profile image
Sanjayshr • Edited

"combination sounds kind of great" ?? Can you explain more why you moving your python web app to functional programming? I also hearing more about functional programming. And how phoenix will help and Python-Django not.

Collapse
 
michaeljones profile image
Michael Jones • Edited

Hey, thanks for asking. I'm not an expert in this stuff but I'll try to explain a few relevant points.

  • The project doesn't really require changes from Django. Python & Django are perfectly good so this isn't motivated by any hard requirements.
  • It is a side project so I can use it to explore new technology and learn new languages. This isn't a high pressure production environment where there are real consequences to my choice.
  • I would like to change to functional programming as I have been writing a lot of Elm code at work and really enjoy the functional approach. I find it to be clearer to understand and easier to maintain. This is mostly due to pure functions and immutable data structures. These make the code easier to reason about and remove some potential bugs. Elm also benefits from a fantastic type system which also catches lots of errors.
  • Python is not great for functional programming. It doesn't have built in support for immutable data structures and it limits anonymous/lambda functions to single expressions so they are not very powerful. It is a great programming language but functional programming is not a core goal for it so they have made different choices.
  • Django has quite an object oriented approach to programming. Particularly in the view layer. They have function views but it feels like a lot of their attention is on the class based views. I written quite a few of those now and I struggle to look at the class hierarchies that result and understand everything that is going on.
  • I have attempted to write my own functional pipeline-based views framework for Django and it is promising in some small ways but I always end up unhappy with what Python will let me do.
  • I think it is quite logical to think of a webserver as a function program. It is almost one big function that takes a request and returns a response. Object-oriented programming makes a huge amount sense in some situations like simulations & games but in my mind it doesn't apply so well to web servers. In this respect the Phoenix approach of modelling a pipeline of functions to handle a request feels like a very natural fit.
  • Even if there are parts of the system that are well modelled by stateful 'objects', Elixir & Erlang provide mechanisms for achieving that in a functional manner.
  • There are also reasons to be very excited about the Elixir ecosystem. Mostly that it is built on the Erlang Virtual Machine which is specifically designed to tackle some interesting and relevant problems like being distributed and reliable (fault tolerant.) I have been watching talks and reading articles about Erlang & Elixir for a while and I am keen to gain experience in the ecosystem. The Zen of Erlang is a particularly interesting description of it. There are also some talks from Greg Young where he describes working in the Erlang ecosystem and some of the benefits.
  • I'm also excited by the fact that Phoenix works with Cowboy which is the standard Erlang webserver and can handle HTTP requests & websockets from the same stack. I'm currently running Django with Gunicorn which is fine but feels like less of an integrated solution and then websocket support also feels like a second class citizen. Though I completely acknowledge that I have not used websockets in Django yet.

I hope some of that shows my state of mind and understanding of the situation. As I said, Django is still a great choice for my project but for reasons of personal interest and growth I'd like to try something else and Elixir seems like an exciting alternative.

Collapse
 
sanjay555 profile image
Sanjayshr

This is neat !! Got a lot on the functional side. It's amazing to read the comment. Thankx for responding is such positive way. Looking forward to read more from you.