Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.8 & Django Crispy Forms: Is there a simple, easy way of implementing a Date Picker?

There are an awful lot of date/datetime picker implementations out there. Are there any that integrate with Django and Crispy Forms particularly well, and how are they used?

I'm looking to minimise development effort, maximise simplicity, and make use of Django localisation.

A Django/Crispy standard output for a date field:

<input class="dateinput form-control" id="id_birth_date" name="birth_date" type="text" value="21/07/2015">

In the Model:

birth_date = models.DateField(verbose_name='D.O.B', auto_now_add=False, auto_now=False)

Thanks!

EDIT:

Profile Model:

from django.db import models

class Profile(models.Model):
    birth_date = models.DateField(verbose_name='D.O.B', auto_now_add=False, auto_now=False)

Profile Form:

from django import forms
from profiles.models import Profile


class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        birth_date = forms.DateField(
            widget=forms.TextInput(
                attrs={'type': 'date'}
            )
        )

Profile View:

from django.shortcuts import get_object_or_404
from django.contrib.auth import get_user_model
from profiles.models import Profile
from profiles.forms import ProfileForm

User = get_user_model()

def edit(request):
    profile = get_object_or_404(Profile, user=request.user)
    form = ProfileForm(request.POST or None, request.FILES or None, instance=profile)
    if request.method == 'POST' and form.is_valid():
        form.save()
    context = {
        'form': form,
        'page_title': 'Edit Profile',
    }
    return render(request, 'profiles/edit.html', context)

Profile Edit Template:

<form enctype="multipart/form-data" method="POST" action=".">
    {% csrf_token %}
    {{ form }}
    <input class='btn btn-primary' type="submit" value="Submit" />
</form>
like image 499
StringsOnFire Avatar asked Jul 21 '15 19:07

StringsOnFire


People also ask

What is current version of Django?

With the release of Django 3.0, Django 2.2 has reached the end of mainstream support. The final minor bug fix release (which is also a security release), 2.2. 8, was issued today. Django 2.2 is an LTS release and will receive security and data loss fixes until April 2022.

Which version of Django is best?

What Python version should I use with Django? ¶ Since newer versions of Python are often faster, have more features, and are better supported, the latest version of Python 3 is recommended.

Is Django 1.11 still supported?

Django 1.11 is designated as a long-term support release. It will receive security updates for at least three years after its release. Support for the previous LTS, Django 1.8, will end in April 2018.

How do I use Django version?

Simply type python -m django --version or type pip freeze to see all the versions of installed modules including Django.


2 Answers

Not an answer for a crispy forms but newer browsers will put in a date picker for you as long as the input has an attribute type of date. This falls into the category of minimal development effort.

date_field = forms.DateField(
    widget=forms.TextInput(     
        attrs={'type': 'date'} 
    )
)                                           
like image 90
Austin Avatar answered Sep 19 '22 14:09

Austin


django.forms.SelectDateWidget is simple and flexible:

date_field = forms.DateField(
    widget=forms.SelectDateWidget(years, months, empty_label)
)

It even includes its own template file.

like image 35
highpost Avatar answered Sep 20 '22 14:09

highpost