首页 > 如何在前台中使用django admin中的日历widget?

如何在前台中使用django admin中的日历widget?

  1. 我看官方有对表单素材有介绍
    (https://docs.djangoproject.com/en/1.9/topics/forms/media/)

Assets and Django Admin

The Django Admin application defines a number of customized widgets for calendars, filtered selections, and so on. 
These widgets define asset requirements, and the Django Admin uses the custom widgets in place of the Django defaults. 
The Admin templates will only include those files that are required to render the widgets on any given page.

If you like the widgets that the Django Admin application uses, feel free to use them in your own application! They’re all stored in django.contrib.admin.widgets.

所以引用django.contrib.admin.widgets文件将django后台页面中自带的日历widget在前台中使用。

但是无论怎么修改前台的表单都是无动于衷。想请各位帮忙看下。

  1. 代码如下:

class Person(models.Model):
    ...
    date_of_starting_working = models.DateField('参加工作时间')
    ...
    
forms.py

from django import forms
from HRSystem.models import Person
from django.contrib.admin import widgets

class PersonForm(forms.ModelForm):
    class Meta:
        model = Person
        exclude = ['is_deleted','pub_date','modify_date',]
        widgets = {
            ......
            'date_of_starting_working' : widgets.AdminDateWidget(),
        }
person_form.html
        ...
        <head>
        ...
        {{form.media}}#引入控件所需的js,css
        ...
        </head>
        ...
        <form role="form" method="post" enctype="multipart/form-data">
            {% csrf_token %}
            {{ form }}
            <input type="submit" value="submit">
        </form>
        ...
  1. 按照这个方法做了以后并没有什么效果,鼠标点击上去后并没有出现预想的弹出日历框。

4.是不是以上代码哪里出现了问题,还是哪里引用的不对?望各位不吝赐教!

【热门文章】
【热门文章】