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

179
Vistas
Posting Date Data to Django Model

I was wondering if any of you guys here knew how to fix this error, I have been dealing with it for quite a few hours, it has to do with posting json date (a date from a html date picker) to a backend model using the django web framework. Please let me know if my question is unclear.

ViewOrders.html

    <form id="form"> 
    <label for="start">Drop Off Date Selector:</label>
    <br>
    <input type="date" id="dropOffDate" name="drop_Off_Date"
        min="2022-01-01" max="3000-12-31">
        <button type="submit" value="Continue" class="btn btn-outline-danger" id="submit-drop-off-date" >Submit Drop Off Date</button>
    </form>

<script type="text/javascript">
        var form = document.getElementById('form')

        form.addEventListener('submit', function(e){
            e.preventDefault()
            submitDropOffData()
            console.log("Drop Off Date submitted...")
        })

        function submitDropOffData() {
            var dropOffDateInformation = {
                'dropOffDate':null,
            }

            dropOffDateInformation.dropOffDate = form.drop_Off_Date.value

            var url = "/process_drop_off_date/"
            fetch(url, {
                method:'POST',
                headers:{
                    'Content-Type':'application/json',
                    'X-CSRFToken':csrftoken,
                }, 
                body:JSON.stringify({'drop-off-date':dropOffDateInformation}),
                
            })
        .then((response) => response.json())
            .then((data) => {
                        console.log('Drop off date has been submitted...')
                        alert('Drop off date submitted');
                        window.location.href = "{% url 'home' %}"
                })
        }
</script> 

Views.py

def processDropOffDate(request):
    data = json.loads(request.body)

    DropOffDate.objects.create(
        DropOffDate=data['drop-off-date']['dropOffDate'],
    )

    return JsonResponse('Drop off date submitted...', safe=False) 

Models.py

class DropOffDate(models.Model):
    dropOffDate = models.CharField(max_length=150, null=True)

    def __str__(self):
        return str(self.dropOffDate)

Errors

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The fix to this was that I had created an object with the wrong field name so the Json repsonse was invalid.

class DropOffDate(models.Model):
    dropOffDate = models.CharField(max_length=150, null=True)

    def __str__(self):
        return str(self.dropOffDate)

Changed to:

class DropOffDate(models.Model):
    DropOffDate = models.CharField(max_length=150, null=True)

    def __str__(self):
        return str(self.DropOffDate)
about 4 years ago · Juan Pablo Isaza Denunciar
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