1

Good day,

I'm trying "create" a DatePicker for one of my Inputfields in Django but it's not working!

In my models.py:

class Customer(models.Model):
    ...
    name = models.CharField()
    date = models.DateField()

In my views.py:

def Page(request):
    CustomerFormSet = modelformset_factory(Customer, fields='__all__')
    formset = CustomerFormSet (queryset=Customer.objects.none())
    ...
    context = {'formset': formset}
    return render(request, 'app/base.html', context)

In my template:

{% extends 'app/base.html' %}
{% load widget_tweaks %}

<form actions="" method="POST">
      {% csrf_token %}
      {{ formset.management_form }}
      {% for form in formset %}
          {{ form.id }}

        ...
        {% render_field form.name class="form-control" %}
        ...
        {% render_field form.date class="form-control" %}
        ...

Now my first Inputfield works fine! It returns a fitting Field in Bootstraps "Form-Group"-Layout. But my InputField for Dates remains a simple TextInput with no calendar apearing to choose from.

My Question is: am I doing something wrong or is it still impossible to obtain such a function in this way?

Thanks and a nice evening to all of you.

3
  • I have a similar problem. I tried adding type=date to the render_field tag, but that only works some of the time. Commented Aug 10, 2020 at 16:11
  • @tyronen Same here! This usually solved my problem: "{% render_field form.date class="form-control" type="date" placeholder="DD.MM.YYYY" %}". But as you mentioned, sometimes it doesn't seem to work. Don't know why, though! Commented Aug 11, 2020 at 19:56
  • My problem went away when I upgraded to the latest version of django-widget-tweaks. I think there were a number of bugs in older versions. Commented Aug 16, 2020 at 10:34

2 Answers 2

1

If you ara using ModelForm try:

from django import forms

class DateInput(forms.DateInput):
    input_type = 'date'

class DataTreinoForm(forms.ModelForm):
    class Meta:
        model = models.YOURMODEL
        fields = _all_
        widgets = {
            'dateField': DateInput
        }

The default format is mm/dd/yyyy. I don't know how to change it in this way.

Sign up to request clarification or add additional context in comments.

Comments

1

I just solved this too. Add type="date" to the render_field.

{% render_field form.date type="date" class="form-control" %}

You can add any input tag attributes here which is convenient because

  1. Modify form appearance on the template instead of forms.py, which is conceptually consistent
  2. When you save templates, it doesnt reload the app, so faster testing with html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.