Django & Site Structure

Posted by robj3d3 on Aug. 4, 2020, 5:52 p.m.

Somehow you landed on this page. Does it matter how? No, not really. Well, not at all actually. Whether you're a lucky Internet surfer, someone stalking all of my socials who happened to stumble upon this goldmine or a lecturer marking my bridging coursework (yes, this blog was initially developed for my bridging coursework - an alternative to the first year exams which were cancelled as a result of the COVID-19 pandemic)...

However you got here, ultimately you might be wondering how the website is even set up. How am I even hosting it (for free)? What did I use to set it up in the first place, and how did I get it looking so snazzy?

Well, let's cut to the chase. For the bare-bones (back-end) of the site, I used Django. This is a free-to-use Python-based and open-source web framework.

Fundamentally, the site structure isn't very complicated. It's comprised of a set of tools and pre-written components that make working with databases, dynamic (as opposed to static) pages and communication between the user and database that much easier! These can be divided into three major parts: Django Models, Django Templates and Django Views.

Essentially the Django app that is running all of the functionality that you're interacting with is configured to work with a small SQLite database (defined using models written in Python - primarily blog post and comment models), which serves data to you using dynamic page templates which are populated using data from the database.

And how does the site know what data to load for you? Well, that's where views come in. These are the clockwork that take the URL you used to reach the site (so on the current page you're reading this blog from, that's /post/1/), and determine what data to query from the database, returning it with the template to be displayed for you. Continuing with the example of this current page, it will take the ID '1' in the URL and query the database for the blog post associated with that ID... which is the blog post you're reading right now!

As for hosting, I've got a version of the code for this website hosted on GitHub, which I'm then pulling from into a server hosted by PythonAnywhere. PythonAnywhere is a web hosting service great for hosting small Python projects on, and I was encouraged by my University to use this - so that's exactly what I'm doing!

Oh, and as for the design, that's just a bit of CSS that I pulled together from different sources online. It's just like using a Python library such as Django - make your life easier where possible by using code written by someone else!

Prior to this I had a fair bit of experience developing websites using Flask (an alternative Python-based web framework) from using it in my A-level Computer Science coursework (ironic, I know). In addition, I've spent my COVID-19 summer in part working as a software engineer intern at a startup based in the US. Through this, I've been learning React.js, FastAPI as well as a bunch of cool DevOps-related technologies such as Docker, Google Cloud Platform and Firebase!

So, in all honesty, it was quite a painful experience developing this site. A lot of the features Django was implementing I wished I was using FastAPI or Flask for. But despite this, I still learnt a bit from it. And now I can say I know most of the popular Python web frameworks, so that's pretty cool.

One day I'll more than likely re-haul this site, and hopefully have a little more life experience to make these blog posts sound a bit more... enthusiastic?

Add comment

No comments here yet :(