Search code examples
pythondjangoscreenshot

How to output my images into html documents using python (Django)


please i need help with my django project. The code i have here is taking screenshots and saving it in my media folder so i want to output the pictures in my html page but it's giving me issues, i think i'm missing a lot.

Here is my python code which is taking the screenshots

from django.http import HttpResponse
import time,random,pyautogui
from django.db import models
import sys,os
from shots.models import pictures
from shots.forms.forms import DocumentForm
from django.conf import settings


def button(request):
    return render(request,'index.html')

def output(request):
    while True:
        myScreenshot = pyautogui.screenshot()
        name = random.randrange(1,1000)
        full_name = str(name) + ".jpg"
        filepath = settings.MEDIA_ROOT + '\shots'
        full_path = filepath + full_name 
        saver = myScreenshot.save(full_path)
        # generate a random time between 120 and 300 sec
        random_time = random.randrange(3,6)
        # wait between 120 and 300 seconds (or between 2 and 5 minutes)
        time.sleep(random_time)
        myScreenshots2 = []
        myScreenshots2.append(saver)
        # return (myScreenshots2)
        return HttpResponse(request,'task.html',saver)

def stopshot(request):
    os.system("pause")```

Solution

  • Python code runs on the server, and user is using a client to connect to the server.When you want to take screenshots it should be done by client not the server, since server is not users computer.

    Check out this question to see how you can take screenshots from client using js.