DateField与日历Django

时间:2013-04-04 05:34:04

标签: django django-forms django-admin django-views

我想使用django-admin面板中的某种日历来选择日期。我试图使用它,但它没有显示日历。

aDate = forms.DateField(widget = AdminDateWidget)

1 个答案:

答案 0 :(得分:0)

forms.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>