How to Integrate my Python with Django and HTML

Viewed 26

Hi I'm extremely new to Django, like yesterday new. I was scouring the web for hours searching for a way to integrate a simple Python project I made to Django in order to keep me on track. I want to use Django as I'm going to be using Django in my classes soon and would like to learn. Is there any way I can do this?

WANTS: I want to use Python for my backend & Django framework to link some HTML, CSS, and possible JavaScript to make thing look nice.

Python Timer Project

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()

I have already integrated Django and started the project (created a urls.py and modified views to get HTTPResponse) I need to know how I can implement my python code and how I can connect it to the HTML from Django.

If this question was confusing please ask questions I am a beginner and I might not have said things correctly.

1 Answers

This is a CLI app, to make it a django app, you have to set up some views in html, and link the django views to your python application, this sounds quite complicated, but don't worry, it is as simple as writing normal HTML files ^^ Django provides a lot of tools to help you build forms and accept an input from your website users, and then process with the data you got from the inputs.

These are 2 links to help you start with;

Good luck with your python journey !

Related