【问题标题】:How to Integrate my Python with Django and HTML如何将我的 Python 与 Django 和 HTML 集成
【发布时间】: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


【解决方案1】:

这是一个 CLI 应用程序,要使其成为 django 应用程序,您必须在 html 中设置一些视图,并将 django 视图链接到您的 python 应用程序,这听起来很复杂,但不要担心,就像写一样简单普通的 HTML 文件 ^^ Django 提供了许多工具来帮助您构建表单并接受来自网站用户的输入,然后处理您从输入中获得的数据。

这些是帮助您开始的 2 个链接;

祝你的蟒蛇之旅好运!

【讨论】:

    【解决方案2】:

    要将您的 Django 后端链接到前端 HTML,您应该使用Django HTML templates,它允许您在 HTML 中使用 Django 语法将后端逻辑和值应用到前端

    很好地遵循文档以了解django views here

    如果您想了解如何从 Django 渲染 HTML 并使用 Django 模板引擎,请查看这两个地方

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 2018-09-11
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-25
      • 1970-01-01
      相关资源
      最近更新 更多