1

我想使用 django-admin 面板中的一种日历来选择日期。我尝试使用它,但它不显示日历。

aDate = forms.DateField(widget = AdminDateWidget)
4

1 回答 1

0

表格.py

from django import forms
from django.contrib.admin import widgets

from .models import MyModel         

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['aDate'].widget = widgets.AdminDateWidget()

在您的模板中添加这些 css 和 js

{% load static %}

<link rel="stylesheet" type="text/css"
    href="{% static 'admin/css/forms.css' %} "/>
<link rel="stylesheet" type="text/css"
    href="{% static 'admin/css/base.css' %} "/>
<link rel="stylesheet" type="text/css"
    href="{% static 'admin/css/global.css' %}"/>
<link rel="stylesheet" type="text/css"
    href="{% static 'admin/css/widgets.css' %}"/>


<script type="text/javascript"
    src="/admin/jsi18n/"></script>
<script type="text/javascript"
    src="{% static 'admin/js/core.js' %}"></script>
<script type="text/javascript"
    src="{% static 'admin/js/admin/RelatedObjectLookups.js' %}"></script>
<script type="text/javascript"
    src="{% static 'admin/js/jquery.js' %}"></script>
<script type="text/javascript"
    src="{% static 'admin/js/jquery.init.js' %}"></script>
<script type="text/javascript"
    src="{% static 'admin/js/actions.js' %}"></script>
<script type="text/javascript"
    src="{% static 'admin/js/calendar.js' %}"></script>
<script type="text/javascript"
    src="{% static 'admin/js/admin/DateTimeShortcuts.js' %}"></script>
于 2013-04-04T06:05:10.617 回答