Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!