Django - Setting date as date input value

18,981

Solution 1

The HTML date should take the format YYYY-MM-DD. So you have to make a conversion using the {{ value|date:"D d M Y" }} command.

Your code will be:

 <input type="date" class="form-control" id="date" value="{{placement.date|date:"Y-m-d" }}">

HTML documentation here: http://www.w3.org/TR/html-markup/input.date.html#input.date.attrs.value

Django date documentation here: https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#date

Solution 2

Noticed a small typo in the above answer -- the date format is in double quotes, inside of a set of double quotes. Should be single around 'Y-m-d' to work!

<input type="date" class="form-control" id="date" value="{{placement.date|date:"Y-m-d" }}">

should be:

<input type="date" class="form-control" id="date" value="{{placement.date|date:'Y-m-d' }}">

Share:
18,981
Pictraz
Author by

Pictraz

Updated on June 26, 2022

Comments

  • Pictraz
    Pictraz almost 2 years

    I'm trying to set a date as the value of a date input in a form. But, as your may have guessed, it's not working.

    Here's what I have in my template:

    <div class="form-group">
           <label for="date" class="col-md-3 control-label">Date</label>
        <div class="col-md-9">
            <input type="date" class="form-control" id="date" value="{{placement.date}}">
        </div>
    </div>
    

    And this is the view that calls it as well as the Placement model:

    class Placement(models.Model):
        student = models.ForeignKey(Student)
        email = models.EmailField(max_length=254)
        fname = models.CharField(max_length=50)
        sname = models.CharField(max_length=50)
        cname = models.CharField(max_length=100)
        position = models.CharField(max_length=50)
        house = models.CharField(max_length=50, blank=True)
        street = models.CharField(max_length=50)
        town = models.CharField(max_length=50)
        county = models.CharField(max_length=50)
        postcode = models.CharField(max_length=8)
        phone = models.CharField(max_length=20)
        length = models.IntegerField(null=True)
        category = models.CharField(max_length=50)
        date = models.DateField(null=True)
        confirmed = models.BooleanField(default=False)
        completed = models.BooleanField(default=False)
        created = models.DateTimeField(null=True)
    
    def view_placement(request, placement_id):
        school = School.objects.get(pk=request.session['school'])
        context = {'school':school}
        if request.session['utype'] == 'a':
            context['user'] = Administrator.objects.get(pk=request.session['user'])
            context['placement'] = Placement.objects.get(pk=placement_id)
            return render(request, 'workxp/admin/view_placement.html', context)
    

    But it doesn't display the date. Just an empty date input...

    How can I fix this?

    Thanks!

    • warath-coder
      warath-coder over 9 years
      you will have to give us your view.py code that calls this template.
    • django-renjith
      django-renjith over 9 years
      can you upload your views [ and model details becoz here mostely we did like this means we will get the date so, please add that also
    • Pictraz
      Pictraz over 9 years
      Added both model and view :)
    • aberna
      aberna over 9 years
      @Pictraz Does the proposed solution work for you ?
  • Pictraz
    Pictraz over 9 years
    Thanks! Just tried it out! Although the "Y M D" format didn't work, I changed it to the default format the HTML accepts by using "Y-m-d", and that put it in fine!
  • aberna
    aberna over 9 years
    Good. strange about the exact format needed. the "Y M D" worked fine for me