Django 表单和缓存控制问题
我正在 django 中填充表单选择字段的选择,这是一个年份选择字段。我从数据库中获取年份并将它们放入该字段的元组列表中。我的代码如下所示
def get_years():
choices = []
years = []
for poll in Poll.objects.all().order_by('created'):
years.append(poll.created.year)
for year in list(set(years)):
choices.append((year, year))
return choices
,我的表单字段如下所示
year = forms.ChoiceField(choices=get_years())
问题是,当我在浏览器中看到它时,根据数据库,年份列表很好,但是当我更改数据库中的某些日期时,年份选择列表不会更新。我尝试过 width @cache_control(no_cache=True)
装饰器,但不起作用。有什么想法吗?
提前致谢!
I'm populating the choices of a form choicefield in django, it's a year select field. I get the years from the database and put them in a list of tuples in the field. My code looks like this
def get_years():
choices = []
years = []
for poll in Poll.objects.all().order_by('created'):
years.append(poll.created.year)
for year in list(set(years)):
choices.append((year, year))
return choices
and my form field looks like this
year = forms.ChoiceField(choices=get_years())
The problem is that when I see it in the browser, the year list is fine according to the database, but when I change some date in database, the year select list doesn't update. I've tried width @cache_control(no_cache=True)
decorator, but doesn't work. Any ideas?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
初始化表单实例时更新年份。
Update the years when you initialize a form instance.