【发布时间】:2021-02-18 11:00:45
【问题描述】:
我不明白小部件的工作原理。
我尝试了这个最小的例子:
在我的 forms.py 中
class PartialResetForm(forms.Form):
date = forms.DateField(
label="Starting date",
widget=AdminDateWidget()
)
在我的管理员/intermediary_reset_page.html
{% extends "admin/base_site.html" %}
<!--Loading necessary css and js -->
{{ form.media }}
{% block content %}
<form action="" method="post">{% csrf_token %}
<!-- The code of the form with all input fields will be automatically generated by Django -->
{{ form }}
<!-- Link the action name in hidden params -->
<input type="hidden" name="action" value="custom_action" />
<!-- Submit! Apply! -->
<input type="submit" name="apply" value="Submit" />
</form>
{% endblock %}
在我的 admin.py 中作为动作的定义
def custom_action(self, request, queryset):
form = PartialResetForm()
return render(request, "admin/intermediary_reset_page.html", {
"items": queryset, "form": form
})
现在我不关心查询集,这将是我的下一个主题。通过这个简单的示例,我想要一个日历来帮助选择日期,但只出现了一个 TextInput。我相信这是因为 AdminDateWidget 继承自 TextInput。
我的问题是它为什么不显示为日历?我导入了媒体并声明了我的小部件,我不明白我还应该做什么。
【问题讨论】: