【问题标题】:Django AdminDateWidget apprearing as TextInputDjango AdminDateWidget 显示为 TextInput
【发布时间】: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。

我的问题是它为什么不显示为日历?我导入了媒体并声明了我的小部件,我不明白我还应该做什么。

【问题讨论】:

    标签: python django forms


    【解决方案1】:

    你应该声明类型

    AdminDateWidget(attrs={'type': 'date'})
    

    应该够了;)

    【讨论】:

      猜你喜欢
      • 2012-12-30
      • 2020-03-12
      • 1970-01-01
      • 2019-03-10
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      相关资源
      最近更新 更多