Python生成Django HTML
对于这个超级基本的问题我很抱歉,但是我最难使用Django将Python变量放入我的HTML文件中Python生成Django HTML,python,html,django,templates,tags,Python,Html,Django,Templates,Tags,对于这个超级基本的问题我很抱歉,但是我最难使用Django将Python变量放入我的HTML文件中 #views.py ?? def my_fucntion(temp): F = temp*(9.0/5)+32 print F weather = my_function(20) 如何通过Django将其添加到HTML文件中 <!-- results.html --> <p>The temp is {{ weathe
#views.py ??
def my_fucntion(temp):
F = temp*(9.0/5)+32
print F
weather = my_function(20)
如何通过Django将其添加到HTML文件中
<!-- results.html -->
<p>The temp is {{ weather }} Fahrenheit today.</p>
如果说“我的函数”未定义,则会出现错误
再说一次,我在这方面很新,所以简单的一步一步或如何将是非常有帮助的。我已经在网上搜索了几天了,我正在努力工作。我读了Django的文档,但很快就迷路了
这似乎是一个非常强大的工具,我只想知道如何让我的一些Python脚本以HTML格式显示
谢谢
编辑:
谢谢你提供的信息,但这对我来说仍然不起作用。当我向上拉HTML时,标签是空白的。这是我的密码:
转换温度
def my_function(temp):
F = temp*(9.0/5)+32
return F
views.py
...
import convert_temp
...
def weather(request):
temp = convert_temp.my_function(20)
return render(request, "polls/results.html", {"weather": temp})
results.html
...
<p>The temp is {{ weather }} Fahrenheit today.</p>
。。。
今天的温度是华氏度
当我加载页面时,结果是“今天的温度是华氏度。”再次感谢
weather=my_函数(20)
应该在您的weather函数中,并且不应该被命名为weather如果您使用的是weather函数名,那么my_函数应该返回而不是打印:
def my_function(temp):
F = temp*(9.0/5)+32
return F
def weather(request):
temp = my_function(20)
return render(request, "detail.html", {"weather": temp})
Django只是Python。如果您已经有了大量Python函数,那么您可能已经知道Python作用域规则、导入等。只需遵循相同的规则,在需要的地方导入函数,然后在视图中调用它们。谢谢。我的模式没有指向正确的位置。
def my_function(temp):
F = temp*(9.0/5)+32
return F
def weather(request):
temp = my_function(20)
return render(request, "detail.html", {"weather": temp})