Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我可以´;t返回烧瓶上的日期和页脚函数_Python_Html_Function_Flask - Fatal编程技术网

Python 我可以´;t返回烧瓶上的日期和页脚函数

Python 我可以´;t返回烧瓶上的日期和页脚函数,python,html,function,flask,Python,Html,Function,Flask,我是一个初学者,考虑到这一点,所以,我需要传递页脚和日期函数,但不知怎么的,我忽略了这里的要点 这里也是一样,我不能返回日期函数,我不知道应该怎么做。谢谢你的帮助。非常感谢 @app.route("/index.html/") def Home(): current_time = datetime.datetime.now() return render_template("index.html", current_time = current_time) if

我是一个初学者,考虑到这一点,所以,我需要传递页脚和日期函数,但不知怎么的,我忽略了这里的要点

这里也是一样,我不能返回日期函数,我不知道应该怎么做。谢谢你的帮助。非常感谢

    @app.route("/index.html/")
def Home():
    current_time = datetime.datetime.now()
    return render_template("index.html", current_time = current_time)





if "Timer Workout" == "__main__":
    app.run()

函数没有返回任何内容,因为它没有被调用

您希望在哪里返回日期/时间

如果在/index.html中,您应该执行以下操作:

@app.route("/index.html/")
def Home():
    current_time = datetime.datetime.now()
    return render_template("index.html", current_time=current_time)
然后在模板中,您可以在HTML代码中添加变量,例如:

Current Date and Time: {{ current_time }}}
基本上,您可以在python主脚本中定义变量值。将它们作为参数传递给render_template函数,然后在模板中使用
{{your_var}}


看看这里:

我也没有时间,但我现在可以调查一下。 我想你是迷路了,因为你有两个视图和两个不同的模板。我想,就目前而言,您只需要渲染一页

现在你的项目应该是这样的:

<html>
<head>
    <title>Index page</title>
</head>

<body>
Current date/time is : {{ current_time }}
<br><br>
The footer would be just below : <br>
{{ footer }}
</body>
</html>
您的
mainscript.py
是:

from flask import Flask, render_template
from datetime import datetime

app = Flask("Timer Workout")

@app.route("/")
def Landing():
    return render_template("Landing_Page.html")

def footer():
    footerdb = open("footer.txt")
    for i in range (3):
        footerdb.write("footer.txt" + " by Carlos ")
    footerdb.close()
    return render_template("Landing_Page.html", footerdb)

@app.route("/index.html/")
def Home():
    current_time = datetime.datetime.now()
    return render_template("index.html", current_time = current_time)

if "Timer Workout" == "__main__":
    app.run()
然后应该有两个HTML文件,
Landing\u page.HTML
index.HTML

但是,我认为您正在尝试使用页脚和当前日期和时间呈现一个且仅一个页面

为此,我将给你一个应该有效的例子。只需删除您的
footer()
函数,该函数有缺陷且未在任何地方调用,我们将只进行一次路由以呈现一个模板

在您的
mainscript.py
中:

from flask import Flask, render_template
from datetime import datetime


app = Flask("Timer Workout")

@app.route("/")
def home():
    current_time = datetime.datetime.now()
    footer = "Placeholder string just for debugging"
    return render_template("index.html", current_time = current_time, footer = footer)


if "Timer Workout" == "__main__":
    app.run()
然后在
index.html
文件中放入如下内容:

<html>
<head>
    <title>Index page</title>
</head>

<body>
Current date/time is : {{ current_time }}
<br><br>
The footer would be just below : <br>
{{ footer }}
</body>
</html>

索引页
当前日期/时间为:{Current_time}


页脚就在下面:
{{footer}}
现在,当您访问
yourwebsite.com/
时,您将看到
index.html
正确呈现。
当然,您应该找到一种更好的方法来添加页脚,您将使用
extends
标记查看模板继承。但是现在,请尝试我的示例,以了解应该很容易操作的基本知识。

你说的“传递函数”是什么意思?RDA抱歉,返回它。非常感谢你的帮助,非常感谢。但是当我尝试此操作时,它不会打开索引,页脚也不会作为函数返回。我确信这是一个初学者的语法错误,我将向您显示“``@app.route(“/”)def Landing():return render_template(“Landing_Page.html”)def footer():footerdb=open(“footer.txt”):footerdb.write(“footer.txt”+“by Carlos Teixeira”)footerdb.close()返回render_template(“Landing_Page.html”,footerdb)``@app.route(“/index.html/”)def Home():current_time=datetime.datetime.now()返回render_模板(“/index.html/”,current_time=current_time)好的,谢谢。但我实际上需要两个呈现两个文件,一个带有多个函数的登录页,包括带有页脚的on,它使用我拥有的数据文件更改时间,然后是索引页,在索引页中,我有一个计时器,在该页上运行datetime模块