Till now, we have been hard coding HTML directly in our Python code, like this:
def current_datetime(request):
now = datetime.datetime.now()
html = "<html> <body> It is now %s. </body></html>" %now
return HttpResponse(html)
Although this technique was convenient for the purpose of explaining how views work, it is not a good idea to hard-code HTML directly into our views. Here is why:
For these reasons, it is much cleaner and more maintainable to separate the design of the page from the Python code itself. We can do this with Django's template system, which we discuss further.
def current_datetime(request):
now = datetime.datetime.now()
html = "<html> <body> It is now %s. </body></html>" %now
return HttpResponse(html)
Although this technique was convenient for the purpose of explaining how views work, it is not a good idea to hard-code HTML directly into our views. Here is why:
- Any change to the design of the page requires a change to the Python code. The design of a site tends to change far more frequently than the underlying Python code, so it would be convenient if the design could change without needing to modify the Python code.
- This is only a very simple example. A common webpage template has hundreds of lines of HTML and scripts. Untangling and troubleshooting program code from this mess is a nightmare. (cough-PHP-cough)
- Writing Python code and designing HTML are two different disciplines, and most professional web-development environments split these responsibilities between separate people (or even separate departments). Designers and HTML/CSS coders shouldn't be required to edit Python code to get their job done.
- It is most efficient if programmers can work on Python code and designers can work on templates at the same time, rather than one person waiting for the other to finish editing a single file that contains both Python and HTML.
For these reasons, it is much cleaner and more maintainable to separate the design of the page from the Python code itself. We can do this with Django's template system, which we discuss further.
No comments:
Post a Comment