Search code examples
pythondjangoformsdatefield

How to put the default datefield in a form with the previous date that was given before django


Well, i am using a datefield. My question is how can i set the default dateField form (in the template) to put it with the previous date that the field has.

class enterprisedata(models.Model):

    ....
    date        = models.DateField(default=timezone.now)
    ....

And this is the template where i am using it:

<!--Date of foundation-->
   <strong>Fecha de fundación:</strong>
      <div class="form-group">
         <input class= "form-control" type="date" name="date" value="{{enterprise.date}}" >
      </div>

See that the form control has the value "enterprise.date", cause i am trying to put the date that it had before the new one...

views.py:

def edit_design(request):

    saveChanges = True
    contact = contactData.objects.get(id=0)
    temp = Templates.objects.get(temp_selected=True)
    enterprise = enterprisedata.objects.get(id=0)
    members = teamMembers.objects.filter(existencia=True)
    design = Available_design_page(isSelected=True)
    indexPage = indexDesign.objects.get(id=0)
    if request.method == 'GET':
        form = EnterpriseForm(instance=enterprise)
        form2 = ContactForm(instance=contact)
        form3 = IndexForm(instance=indexPage)
    else:
        form = EnterpriseForm(request.POST or None, request.FILES or None, instance=enterprise)
        form2 = ContactForm(request.POST or None, request.FILES or None, instance=contact)
        form3 = IndexForm(request.POST or None, request.FILES or None, instance=indexPage)
        if form.is_valid() and form2.is_valid() and form3.is_valid():
            form.save()
            form3.save()
            form2.save()
            return redirect('adminview:save_page')
        else:
            diccionario=request.POST
            print('es invalido: ', diccionario)
        return redirect('adminview:edit_design')
    contexto = {'form':form,
                'form2':form2,
                'form3':form3,
                'members':members,
                'design':design,
                'enterprise':enterprise,
                'contact':contact,
                'indexPage':indexPage,
                }
    return render(request, 'adminview/edit_design.html', contexto)

If you can help me i will appreciate...

Thank you!.


Solution

  • So after doing some searching I found out that, the <input type="date"> is supported only in some browsers and versions, take a look at this link: w3schools it shows the compatibility.

    also there is a format that the value atttribute would take in the date it is YYYY-MM-DD I found this out in another question on SO, check this answer: How to set default value to the input[type=“date”]

    So I guess you must check the data for the date that is getting into the html page through the view, and the format.