2013-04-04 128 views
1

我想使用一種類似於django-admin面板的日曆來選擇日期。我試圖使用這個,但它沒有顯示日曆。帶日曆的日期字段Django

aDate = forms.DateField(widget = AdminDateWidget) 
+0

添加這些CSS和JS你能否詳細說明你的問題有點多。我似乎很難理解你。 – 2013-04-04 05:37:42

+0

datefield的作品,但我想有一個日曆來選擇日期,就像在管理頁面或類似的東西 – 2013-04-04 05:42:09

回答

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() 

在你的模板

{% 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>