Search code examples
pythondjangodjango-modelsdjango-formsdjango-crispy-forms

Why won't my Django template display the correct date listed in the database?


My model contains a datefield, 'valid_until'. Even though an entry contains the date '12/25/2022', the update template literally displays as its contents 'mm/dd/yyyy' when the user tries to modify the date. Why? Also, there seems to be no way to create a placeholder text or even change that date format (it always renders in that same format---even though I specify '%m/%d/%y' in forms.py and settings.py contains the format '%m/%d/%y'). Why? All of these problems seem related to the date picker feature. Are Django forms not compatible with the date picker feature?

settings.py:

TIME_ZONE = 'US/Eastern'
USE_I18N = True
USE_L10N = True
DATE_INPUT_FORMATS = '%m/%d/%y'

models.py:

class DecisionsList(models.Model):
        summary = models.CharField(default="", max_length=75, ` 
 verbose_name="Decision Summary")
        description = models.CharField(max_length=100, verbose_name="Decision Description")
        valid_until = models.DateField(default=timezone.now, verbose_name="Voting End Date")
        date_created = models.DateTimeField(default=timezone.now)   

views.py:

class UpdateDecisionView(LoginRequiredMixin, UpdateView):

  model = DecisionsList
    form_class = DecisionForm
    template_name = "users/update_decision.html"

forms.py:

class DecisionForm(forms.ModelForm):
    class Meta:
        model = DecisionsList
        fields = ['summary', 'description', 'valid_until']
        widgets = {
            'valid_until': forms.DateInput(
                format='%m/%d/%y',
                attrs={'type': 'date', 'placeholder': "Select a date"
                       }),
            'description': forms.Textarea(
                attrs={'line-height': '50px',
                       })
        }

update_decision.html:

{% extends "users/base_users.html" %}

{% load crispy_forms_tags %}
{% load crispy_forms_filters %}


<body>

  {% block content %}

      <div class="nav">
        <a id="company" {% block company %} class="selected" {% endblock %} href=""></a>
      </div>

      <div class="containers database-containers">
        <h2>Update a Decision</h2>
      <form method="post">
        <div class="form-group">
                  {% csrf_token %}
                  {{form.summary|as_crispy_field}}
                  <div class="text-area">
                  {{form.description|as_crispy_field}}
                  </div>
                  <div class="short-field">
                  {{form.valid_until|as_crispy_field}}
                  </div>
            <input type="submit" class="btn" value="Update"/>
              <a href="{% url 'users:company' %}" class="btn">Cancel</a>
        </div>
      </form>
      </div>

  {% endblock %}

</body>

Solution

  • I found a solution that solved all of my Django / DatePicker issues: Following is what I do, no external dependencies at all.:

    Post that provides example of how to use a date picker in Django

    models.py:

    from django.db import models
    
    
    class Promise(models):
        title = models.CharField(max_length=300)
        description = models.TextField(blank=True)
        made_on = models.DateField()
    forms.py:
    
    from django import forms
    from django.forms import ModelForm
    
    from .models import Promise
    
    
    class DateInput(forms.DateInput):
        input_type = 'date'
    
    
    class PromiseForm(ModelForm):
    
        class Meta:
            model = Promise
            fields = ['title', 'description', 'made_on']
            widgets = {
                'made_on': DateInput(),
            }
    my view:
    
    class PromiseCreateView(CreateView):
        model = Promise
        form_class = PromiseForm
    And my template:
    
    <form action="" method="post">{% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Create" />
    </form>