Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
Django html template in a seperate file

My question is more of an etiquette theme rather than anything else. I'm developing a relatively small application that reads XML data and writes to an Oracle DB and outputs to a webpage.

I'm reading through the "The Definitive Guide to Django: Web Development Done Right" book, basically because learning something new is fun I recognise more and more requests for Engineers with Django experience.

In the book, there is a recommendation to keep html templates separetly from the rest of the code (for example in .html files in its own folder).

Are there any advantages in doing so, as opposed to just leaving an html template embedded in my python script?

The template which I'm using is about 15 lines, and uses the data in a generate list to populate the html text:

path_to_be_used = desktop_path + 'XURA_output.html'
f = open(path_to_be_used, 'w')

string = """<html>
<head><title>XURA Messaging</title></head>
<body>
<p>XURA Message contents: the data contained in this page is the Push Notification output retrieved from the XURA server.<br>
Upon arrival this data is subsequently written to the Oracle database.        
"""
f.write(string)

line1 = "<p><pre>TAG".ljust(28) + "|   " + "TEXT".ljust(40) + " </pre>"   
f.write(line1)  

for tag in smsreport_list:
    tagInfo = tag[0]
    textInfo = tag[1]
    loop_line = "<pre>" + tagInfo.ljust(20) + "|   " + textInfo.ljust(40) + " </pre>"  
    f.write(loop_line) 

line2 = """</p></body>
</html>"""

f.write(line2)
f.close()
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Two things that immediately jump in mind about django templates, are the

  1. Template inheritance
    You can have your templates inherit similar behavior between them, something which you cannot normally do.

  2. Render view data on a Template
    In your code you are passing data to your html string, at run time. You can avoid this by utilizing django's template language which has logical operation like if, elseif and loop operations like for. You can also pass data between your html template and your django-views in an efficient and simple manner, which makes your templates more "dynamic".

Have a look here and here for a simple example of both the above cases (also read the whole article).

over 4 years ago · Santiago Trujillo Report

0

John's answer gives two good reasons. Here are two more:

  1. Separating templates from controller code simplifies both. You don't have to know HTML to work on the controller function; you don't need to know Python to work on the templates. Your editor or IDE is also far more likely to provide syntax highlighting, completion, etc. with separate files.

  2. Keeping views and controllers separate (and models, too, though you're not asking about that) often results in code that is more easily tested with automated testing tools like Python's built-in unittest library. Django leverages this library for its own tests. This is partly caused by lower coupling and possibly higher cohesion.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!