输入日期值属性django

时间:2019-05-30 16:20:05

标签: html django date

我正在尝试在HTML中显示数据库中日期字段的值,但该字段仍显示空白文本

在设置日期值时,我尝试使用Django管道。当我甚至在标签中使用完全相同的代码时,它都可以正常工作,但不仅在日期字段中有效

这是我的views.py文件

def all(request):
    employees = Employee.objects.all().order_by('last_name')
    departments = Department.objects.all()
    table = EmployeeTable(employees)
    RequestConfig(request).configure(table)
    context =  {
        'table':table,
        'employees':employees,
        'departments':departments
    }
    if request.method == "POST":
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        name = last_name +' '+first_name
        employee_id = request.POST['employee_id']
        email = request.POST['email']
        department = Department.objects.get(dept_name = request.POST['department'])
        address = request.POST['address']
        employment_type = request.POST['employment_type']
        employment_status = request.POST['employment_status']
        role = request.POST['role']
        marital_status = request.POST['marital_status']
        gender = request.POST['gender']
        join_date = request.POST['join_date']
        end_date = None if len(request.POST['end_date']) ==0 else request.POST['end_date']
        location = request.POST['location']
        credentials = request.POST['credentials']
        passport = request.POST['passport']
        hod = request.POST['hod']
        phone_number = request.POST['phone_number']
        date_of_birth = request.POST['date_of_birth']
        date_added = datetime.now()

这是我的HTML代码

<div class="col-sm-6">
  <div class="form-group">
     <label for='date_of_birth' class="control-label">Date of Birth
       {{employee.date_of_birth|date:'Y/m/d'}}  
       {{employee.date_of_birth|date:'m-d-Y'}}  
       {{employee.date_of_birth|date:'SHORT_DATE_FORMAT'}} <span class="text- 
       danger">*</span>
     </label>
     <input name='date_of_birth' type="date"  class="form-control" value = " 
     {{employee.date_of_birth|date:'m/d/Y'}} " >
   </div>
</div>

Result

1 个答案:

答案 0 :(得分:1)

<input type="date"> [mozilla-doc]value=...属性的格式为:

  

DOMString表示日期,格式为 YYYY-MM-DD ,或者为空。

所以您的<input>元素应该是:

<input type="date" value="{{employee.date_of_birth|date:'Y-m-d'}}" name="date_of_birth" class="form-control">