Django-将日期设置为日期输入值 [英] Django - Setting date as date input value

查看:22
本文介绍了Django-将日期设置为日期输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将日期设置为表单中输入的日期的值.但是,正如您可能已经猜到的那样,它不起作用.

I'm trying to set a date as the value of a date input in a form. But, as your may have guessed, it's not working.

这是我模板中的内容:

<div class="form-group">
       <label for="date" class="col-md-3 control-label">Date</label>
    <div class="col-md-9">
        <input type="date" class="form-control" id="date" value="{{placement.date}}">
    </div>
</div>

这是调用它的视图以及Placement模型:

And this is the view that calls it as well as the Placement model:

class Placement(models.Model):
    student = models.ForeignKey(Student)
    email = models.EmailField(max_length=254)
    fname = models.CharField(max_length=50)
    sname = models.CharField(max_length=50)
    cname = models.CharField(max_length=100)
    position = models.CharField(max_length=50)
    house = models.CharField(max_length=50, blank=True)
    street = models.CharField(max_length=50)
    town = models.CharField(max_length=50)
    county = models.CharField(max_length=50)
    postcode = models.CharField(max_length=8)
    phone = models.CharField(max_length=20)
    length = models.IntegerField(null=True)
    category = models.CharField(max_length=50)
    date = models.DateField(null=True)
    confirmed = models.BooleanField(default=False)
    completed = models.BooleanField(default=False)
    created = models.DateTimeField(null=True)

def view_placement(request, placement_id):
    school = School.objects.get(pk=request.session['school'])
    context = {'school':school}
    if request.session['utype'] == 'a':
        context['user'] = Administrator.objects.get(pk=request.session['user'])
        context['placement'] = Placement.objects.get(pk=placement_id)
        return render(request, 'workxp/admin/view_placement.html', context)

但是它不显示日期.只是一个空的日期输入...

But it doesn't display the date. Just an empty date input...

我该如何解决?

谢谢!

推荐答案

HTML日期应采用YYYY-MM-DD格式.因此,您必须使用 {{value | date:"D d M Y"}} 命令进行转换.

The HTML date should take the format YYYY-MM-DD. So you have to make a conversion using the {{ value|date:"D d M Y" }} command.

您的代码将是:

 <input type="date" class="form-control" id="date" value="{{placement.date|date:"Y-m-d" }}">

此处的HTML文档: http://www.w3.org/TR/html-markup/input.date.html#input.date.attrs.value

HTML documentation here: http://www.w3.org/TR/html-markup/input.date.html#input.date.attrs.value

此处为Django日期的文档: https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#date

Django date documentation here: https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#date

这篇关于Django-将日期设置为日期输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆