Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
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()
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

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).

about 4 years ago · Santiago Trujillo Relatório

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.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda