Sunday, August 19, 2018

Class Based Views in the Django app

The past few releases have seen small to significant changes in the Django web app. And every time I made these changes, the one thing that struck me is how awful the Django code is. At the time I was creating the web app for the circuit simulator, I was just getting into web development and I had little knowledge of best practices, what a REST API was and of course the look and feel of a good website. I just wanted something that could be run off a server.

Now that I have been designing websites for a while and also had positions of full stack web developer, I realize I need to change the code. To begin with in the Django app, I used function based views which results in a lot of redundant and repeated code. I need to convert this to class based views to be able to reuse code and implement DRY.

The next is that for some reason, I wanted to have the minimum number of links in the app. No reason for doing so. But because I had just one link /new-simulation/ for the entire simulation editing and running to see the results, I have incredibly complex and difficult to maintain code in the function view that serves this URL. As an example, for all purposes, I have used submit buttons that have different values or submit buttons with hidden fields to pass information back to the view function. In reality, the only time you need to use a submit button is while submitting a form. To access any other information, only a GET request is needed. To pass information between URL and view function, there are much more convenient methods such as URL parameters. This will result in a several links for each operation performed. But so what? URLs can be plentiful. the views that serve them need to be most efficient.

Next, the look and feel of the app. When I started, I didn't know any frontend development. Now I regularly code in JavaScript and use CSS, Bootstrap and sometimes even combine all these in a frontend framework like Angular. So, I need to bring in Bootstrap and custom CSS pages into this web app for the circuit simulator to make it much more visually appealing.

The migration to Django class based views has started. It is progressing page by page and might be done in another 2 weeks. The front end changes will take another week at most. In a month, a much better web app should be available which I hope will have more engineers using it.

As before, to get regular updates on the project, follow my Facebook page:
https://www.facebook.com/pythonpowerelectronics

And to learn how to use the simulator or access other tutorials, check out my YouTube channel:
https://www.youtube.com/channel/UCxVbKNK18A_a9Ohd0Kb7kNA