为什么我的 Django 模板不能显示数据库中列出的正确日期?
Why won't my Django template display the correct date listed in the database?
我的模型包含一个日期字段,'valid_until'。即使条目包含日期“2022 年 12 月 25 日”,当用户尝试修改日期时,更新模板仍会按字面意思显示其内容 'mm/dd/yyyy'。为什么?此外,似乎无法创建占位符文本,甚至无法更改该日期格式(它始终以相同的格式呈现——即使我在 forms.py 中指定了 '%m/%d/%y' settings.py 包含格式“%m/%d/%y”)。为什么?
所有这些问题似乎都与日期选择器功能有关。 Django 表单是否与日期选择器功能不兼容?
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>
这是 Django 将接受的有效日期格式列表:
[
'%Y-%m-%d', # '2006-10-25'
'%m/%d/%Y', # '10/25/2006'
'%m/%d/%y', # '10/25/06'
'%b %d %Y', # 'Oct 25 2006'
'%b %d, %Y', # 'Oct 25, 2006'
'%d %b %Y', # '25 Oct 2006'
'%d %b, %Y', # '25 Oct, 2006'
'%B %d %Y', # 'October 25 2006'
'%B %d, %Y', # 'October 25, 2006'
'%d %B %Y', # '25 October 2006'
'%d %B, %Y', # '25 October, 2006'
]
如果您使用 JQuery UI 日期选择器,您可以更改 UI 元素的格式。只要确保它符合 Django 接受的内容 - more info
$( ".selector" ).datepicker({
dateFormat: "mm/dd/yy"
});
我找到了解决所有 Django / DatePicker 问题的解决方案:
以下是我所做的,完全没有外部依赖。:
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>
我的模型包含一个日期字段,'valid_until'。即使条目包含日期“2022 年 12 月 25 日”,当用户尝试修改日期时,更新模板仍会按字面意思显示其内容 'mm/dd/yyyy'。为什么?此外,似乎无法创建占位符文本,甚至无法更改该日期格式(它始终以相同的格式呈现——即使我在 forms.py 中指定了 '%m/%d/%y' settings.py 包含格式“%m/%d/%y”)。为什么? 所有这些问题似乎都与日期选择器功能有关。 Django 表单是否与日期选择器功能不兼容?
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>
这是 Django 将接受的有效日期格式列表:
[
'%Y-%m-%d', # '2006-10-25'
'%m/%d/%Y', # '10/25/2006'
'%m/%d/%y', # '10/25/06'
'%b %d %Y', # 'Oct 25 2006'
'%b %d, %Y', # 'Oct 25, 2006'
'%d %b %Y', # '25 Oct 2006'
'%d %b, %Y', # '25 Oct, 2006'
'%B %d %Y', # 'October 25 2006'
'%B %d, %Y', # 'October 25, 2006'
'%d %B %Y', # '25 October 2006'
'%d %B, %Y', # '25 October, 2006'
]
如果您使用 JQuery UI 日期选择器,您可以更改 UI 元素的格式。只要确保它符合 Django 接受的内容 - more info
$( ".selector" ).datepicker({
dateFormat: "mm/dd/yy"
});
我找到了解决所有 Django / DatePicker 问题的解决方案: 以下是我所做的,完全没有外部依赖。:
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>