
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Creating a screenshot taking website in Django
In this article, we will see how to create a screenshot taking website in Django. This website will basically take the screenshot of our screen whenever we click "take screenshot". It then will store the screenshot in our media folder and we can view it whenever we want.
Example
So let's start by creating a project and an app.
In settings.py, in INSTALLED_APPS add your app name, and add this code at the bottom −
MEDIA_URL = '/media/' MEDIA_ROOT = BASE_DIR/'media'
We just setup our basic media folder for our image uploading purpose.
Now in project's urls.py −
from django.contrib import admin from django.urls import path,include urlpatterns = [ path('admin/', admin.site.urls), # this is my app name path('',include('screenshottaker.urls')) ]
Here we simply added our app urls.py
In app's urls.py −
from django.urls import path,include from . import views from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path('', views.home) ]+ static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)
We just rendered our simple view and setup media conical urls.
Install the following modules −
pip install pyautogui pip install pillow
Here we installed libraries that we are going to use.
In views.py, add the following code −
from django.shortcuts import render # Create your views here. import random import pyautogui from django.conf import settings from django.contrib import messages def home(request): if request.method == "POST": ss = pyautogui.screenshot() img = f'myimg{random.randint(1000,9999)}.png' ss.save(settings.MEDIA_ROOT/img) messages.success(request,'screenshot has been taken') return render(request,'home.html',{'img':img}) return render(request,'home.html')
Here in views.py, we simply made pyautogui to take screenshot, then we generated random number with png to create an image name. Then, we saved the image at MEDIA_ROOT which is a media folder. Then, we sent a message to our frontend and the screenshot that we just took.
Now, you need to create a "media" folder at the same level of project and app directory.
Create a templates folder in the app directory at the same level of migration folder and create "home.html" and add the following code −
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=devicewidth, initial-scale=1.0"> <title>Document</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/ dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous"> </head> <body> <style> img{ width: 800px; } </style> {% if messages%} {% for message in messages%} <div class="text-center mt-5">{{message}}</div> {%endfor%} {%endif%} <form method="post" class="text-center">{%csrf_token%} <button class="p-3 btn btnsuccess">Take A Screenshot</button> </form> {% if img%} <div class="text-center p-5 mt-5 container border shadowsm"> <img src="http://127.0.0.1:8000/media/{{img}}" class= "img-fluid"> </div> {%endif%} </body> </html>
Now, we are all done. Here, we created a simple frontend with CSS, and a POST form, which uses Django conditioning for showing the image and the message.
Output
−
- Related Articles
- Django – Making a Django website more human-like using Humanizer
- QR code generating website in Django
- How to add a captcha in a Django website?
- How to add authentication to Django Website?
- How to add authorization to your Django website?
- How to add security to your Django website?
- How to Build your own website using Django in Python
- Is it possible to prevent the users from taking screenshot of webpage?
- How do I prevent Android taking a screenshot when my app goes to the background?
- Adding a DeleteView in Django
- How to programmatically take a screenshot in android?
- How to implement django-material in your Django project?
- How to take a screenshot programmatically in iPhone/iOS?
- Making a Pickle field in Django models
- Making a URL shortner app in Django
