Django在views.py中返回静态HTML?

时间:2016-10-15 06:10:33

标签: python html django http httpresponse

所以我有一个标准的Django项目,其基本视图返回一个简单的HTML确认声明。我可以将视图中的所有HTML定义为一个非常长的字符串,并使用HttpResponse()返回该字符串,我知道它有点unorthodox,但这是一个例子我在想什么:

from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render_to_response

def index(request):
    html = """
<html>
  <body>
    This is my bare-bones html page.
  </body>
</html>
"""
    return HttpResponse(html)

我的相应JSstylesheets将与视图存储在同一目录中。在这个例子中我的应用程序中的py。只是确定:我不会问这是否有效,因为我已经知道答案是肯定的,我只是想知道这种方法是否有任何缺点/缺点,以及为什么不让更多的人这么做此?

2 个答案:

答案 0 :(得分:0)

大多数人不使用它,因为它将Python与HTML混合在一起并且变得非常混乱并且很快失控

答案 1 :(得分:0)

您可以使用内置template renderer来获取工作过滤器/ templatetags / etc