One of the reasons why I was initially attracted to Flask was the ability for extremely simple, basic, websites. If you just want to create an informative site about something, it's almost as easy as wordpress. As you begin to expand your content, however, you're going to need something to help you manage this content. This is what is known as a "Content Management System."
This time, I like Flask because you can easily create your very own Content Management System (CMS). The idea of a CMS is to organize your content for you, making it a little easier to add new content, delete content, or modify existing content. For me, I wanted to have a lot of editing and customization cababilities on my pages, but I still needed a way to organize the orders of the tutorials, under their respective topics, as well as each topic's title and link. I am heading towards 800 tutorials at the time of my writing this, so you can imagine the need for a system to manage this much content!
I settled on using a nice simple Python dictionary where the keys were the tutorial series names, then the lists contained the title to the specific tutorial, followed by a link. Since it is a list, it is also mutable. Instead of referencing hard URLs in our __init__.py file, we can instead reference elements within the dictionary, keeping things dynamic!
This also leaves room in the future for me to add things like tags, suggested tutorials, or anything else to the list in time without breaking anything.
File: content_management.py, server location: /var/www/PythonProgramming/PythonProgramming/content_management.py
def Content(): TOPIC_DICT = {"Basics":[["Introduction to Python","/introduction-to-python-programming/"], ["Print functions and Strings","/python-tutorial-print-function-strings/"], ["Math basics with Python 3","/math-basics-python-3-beginner-tutorial/"]], "Web Dev":[]} return TOPIC_DICT
Now that we have this content_management.py file, we need to reference it. First we import it, then we need to pass the TOPIC_DICT through to our HTML file.
Passing variables through HTML is easy enough via render_template:
File: __init__.py, server location: /var/www/PythonProgramming/PythonProgramming/__init__.py
from flask import Flask, render_template from content_management import Content TOPIC_DICT = Content() app = Flask(__name__) @app.route('/') def homepage(): return render_template("main.html") @app.route('/dashboard/') def dashboard(): return render_template("dashboard.html", TOPIC_DICT = TOPIC_DICT) if __name__ == "__main__": app.run()
Finally, we're ready to make use of this. We use the nav tabs we learned about before, only this time we're using a for loop to iterate through the lists of lists within our dictionary per tutorial series within it.
File: dashboard.html, server location: /var/www/PythonProgramming/PythonProgramming/templates/dashboard.html
{% extends "header.html" %} {% block body %} <div role="tabpanel"> <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> <li role="presentation" class="active"><a href="#basics" aria-controls="basics" role="tab" data-toggle="tab">Basics</a></li> <li role="presentation"><a href="#wdev" aria-controls="wdev" role="tab" data-toggle="tab">Web Dev</a></li> <li role="presentation"><a href="#dan" aria-controls="dan" role="tab" data-toggle="tab">Data Analysis</a></li> </ul> <!-- Tab panes --> <div class="tab-content"> <div role="tabpanel" class="tab-pane fade in active" id="basics"> <ul> {% for t in TOPIC_DICT["Basics"] %} <li><a href="{{t[1]}}">{{t[0]}}</a></li> {% endfor %} </ul> </div> <div role="tabpanel" class="tab-pane fade" id="wdev">web dev</div> <div role="tabpanel" class="tab-pane fade" id="dan">Data analysis!#Q$@!#</div> </div> </div> {% endblock %}