Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
How do I input an image that is already displayed on html?

I'm working with a webcam application and I am trying to integrate it into Django framework.

This is the function that takes a photo(it's javascript in static folder):

    // play sound effect
    shutter.play();

    // take snapshot and get image data
    Webcam.snap( function(data_uri) {
      // display results in page
      document.getElementById('results').innerHTML =
        '<img id="imageprev" src="'+data_uri+'"/>';
    } );
    document.getElementById("camera").outerHTML = "";

    Webcam.reset();
  }

This is a fraction from my template:

<div id="camera"></div>
<button onclick="takeSnapShot()" type="button" id="button1" class="button-1">Take Picture</button>
<form action="" enctype="multipart/form-data" method="POST">
        {% csrf_token %}
        <div name="results" id="results"></div>
        <div class="button-2">
            <button type="submit" href="/">Pateikti</button>
        </div>
</form>

So after picture taken, it displays an image inside < div > tag which id is "results" And now I want to save taken photograph. So I tried POST method, but when I do that, the template reloads, and the image gets lost. So I believe that's why I'm getting this error window. Also usually POST method used when you want to save files in your server which client uploads, so I'm not sure if this is the best way to do this

So my question is: Is there a method to save an image to your server from a template? Or am I using POST method wrong? I don't want to use PHP since I'm still new to programing

views.py for additional code:

from django.shortcuts import render
from .models import photo
from django.core.files.storage import FileSystemStorage
import datetime

# Create your views here.

def index(request):
    if request.method == 'POST':
        
        file=request.FILES['imageprev']
        fs=FileSystemStorage()
        filen = fs.save(datetime.datetime.now(), file)
        return render(request, 'index.html')
    else:
        return render(request, 'index.html')

models.py:

from django.db import models

class photo(models.Model):
    file = models.FileField(upload_to='file')

I hope my problem is understandable, if not, please ask for more info. I will happily provide it.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda