0

I'm trying to save my images to the media root folder which I've named pictures in the media folder. This is my code and I want whenever my function is making the screenshot it should save the screenshots in my media root folder.

from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import get_object_or_404,render,redirect
import time 
import random
from django.db import models
import pyautogui
import sys
from shots.models import pictures
from .models import pictures
from shots.forms.forms import DocumentForm

def button(request):
    import pyautogui
    myScreenshot = pyautogui.screenshot()
    storage=pictures.pictures2
    myScreenshot.save(storage)

    return render(request,'index.html')

def output(request):
    return HttpResponse("""Hello, world. You're at the polls index.
    your Screenshot should start now""")

This is my models.py:

from django.db import models
import random


class pictures(models.Model):
    pub_date = models.DateTimeField('date published')
    pictures2 = models.ImageField(upload_to='pictures')
letsintegreat
  • 3,328
  • 4
  • 18
  • 39
fyunusa
  • 95
  • 1
  • 8

1 Answers1

0

You will find this answer helpful.

The files for FileField and ImageField are uploaded relative to settings.MEDIA_ROOT and should be accessible by the same relative filename appended to settings.MEDIA_URL.


That being said, if you named your settings.MEDIA_ROOT = 'pictures' and you are telling your models to upload_to='pictures' So files uploaded by django will end up in:

**/pictures/pictures/myScreenshot.png** 
import pyautogui

from django.shortcuts import render
from django.http import HttpResponse
from django.conf import settings

from .models import pictures


def button(request):
    myScreenshot = pyautogui.screenshot()

    """ 
    The path and filename are relative o where you execute this script.
    """
    filename = settings.MEDIA_ROOT+'pictures/myScreenshot.png'
    myScreenshot.save(filename)

    return render(request,'index.html')

Your models.py:

from django.db import models
import random


class pictures(models.Model):
    pub_date = models.DateTimeField('date published')
    pictures2 = models.ImageField(upload_to='pictures')
Aerials
  • 4,231
  • 1
  • 16
  • 20
  • i know of that what i what to get is how will i be able to save the files from the screenshot to the root directory – fyunusa Feb 18 '20 at 15:05
  • thank you i tried it but also i can't find the screenshots in my folder... please is there something i'm missing ? – fyunusa Feb 18 '20 at 16:50
  • Are you finding your screenshots anywhere? Please post your MEDIA settings – Aerials Feb 18 '20 at 16:56