Django 1.8及更高版本Django Crispy Forms:有没有简单,容易的方法来实现日期选择器? [英] Django 1.8 & Django Crispy Forms: Is there a simple, easy way of implementing a Date Picker?
问题描述
那里有很多日期/日期时间选择器实现。是否有与Django和Crispy Forms集成得特别好的东西,以及它们如何使用?
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?
我希望最大程度地减少开发工作,最大程度地简化并利用Django本地化。
I'm looking to minimise development effort, maximise simplicity, and make use of Django localisation.
日期字段的Django / Crispy标准输出:
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">
在模型中:
birth_date = models.DateField(verbose_name='D.O.B', auto_now_add=False, auto_now=False)
谢谢!
编辑:
配置文件模型:
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)
配置文件格式:
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'}
)
)
个人资料视图:
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)
配置文件编辑模板:
<form enctype="multipart/form-data" method="POST" action=".">
{% csrf_token %}
{{ form }}
<input class='btn btn-primary' type="submit" value="Submit" />
</form>
推荐答案
较新的浏览器将无法解决脆弱的表单问题只要输入的属性 type
为 date
,就可以为您放置一个日期选择器。属于开发工作量最少的类别。
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'}
)
)
这篇关于Django 1.8及更高版本Django Crispy Forms:有没有简单,容易的方法来实现日期选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!