【发布时间】:2022-09-28 05:33:32
【问题描述】:
嗨,我对 Django 非常陌生,就像昨天一样。我在网上搜索了好几个小时,寻找一种方法来将我制作的一个简单的 Python 项目集成到 Django 中,以保持我的进度。我想使用 Django,因为我很快就会在我的课程中使用 Django 并且想学习。有什么办法可以做到这一点吗?
想要:我想为我的后端和 Django 框架使用 Python 来链接一些 HTML、CSS 和可能的 JavaScript 以使事情看起来不错。
Python 计时器项目
import time
import os
from threading import Thread
def userTimeSet():
while True:
try:
hours = int(input(\"Number of hours? \"))
minutes = int(input(\"\\nNumber of minutes? \"))
if hours > 0 and minutes > 0:
interval_sec = (hours*3600)+(minutes*60)
print(f\"hours and minutes to seconds checking: {interval_sec}\")
elif hours > 0 and minutes == 0:
interval_sec = (hours*3600)
print(f\"hours to seconds checking: {interval_sec}\")
elif hours == 0 and minutes > 0:
interval_sec = (minutes*60)
print(f\"minutes to seconds checking: {interval_sec}\")
else: print(\"invalid.\")
futureTime = timeConfirmation(hours, minutes)
# create and start the daemon thread
print(\'Starting background task...\')
daemon = Thread(target=timeCheck, args=(futureTime, interval_sec,), daemon=True, name=\'Background\')
daemon.start()
print(\'Main thread is carrying on...\')
time.sleep(interval_sec)
print(\'Main thread done running.\')
break
except ValueError:
print(\"Enter a number.\")
def timeConfirmation(hours, minutes):
print(f\"{hours}:{minutes} (hours:minutes)\")
currentDateTime = time.ctime()
print(f\"\\n Current Date and Time: {currentDateTime}\")
timeInSeconds = (hours * 3600) + (minutes * 60)
futureTime = time.time() + timeInSeconds
print(f\"\\n Timer Ends at: {time.ctime(futureTime)}\")
return futureTime
def timeCheck(futureTime, interval_sec):
while True:
time.sleep(interval_sec)
currentTime = time.time()
if time.ctime(currentTime) == time.ctime(futureTime):
alarmNotification()
print(f\"Checking time. {time.ctime(currentTime)}\")
def alarmNotification():
os.system(\"\"\"
osascript -e \'display notification \"{}\" with title \"{}\" sound name \"{}\"\'
\"\"\".format(f\"Current time is: {time.ctime()}\", \"Its Break Time Boi\", \"Boop\"))
userTimeSet()
我已经集成了 Django 并启动了项目(创建了一个 urls.py 并修改了视图以获取 HTTPResponse)我需要知道如何实现我的 python 代码以及如何将它连接到来自 Django 的 HTML。
如果这个问题令人困惑,请问我是初学者的问题,我可能没有正确地说出问题。
-
这是一个 CLI 应用程序。 “将它从 Django 连接到 HTML”是什么意思?
-
我不希望它是一个 CLI 应用程序我想从 html 表单中获取用户条目,然后将其传递给我的后端让它在后台运行,直到计时器达到其结束,有没有办法做那使用Django?
标签: javascript python html css django