日期字段不自动填充值Django

时间:2020-09-15 07:33:30

标签: django django-models django-forms django-views django-templates

我的浏览器(chrome和firefox)doesn`t autofill my Datefield,但在野生动物园工作example

我检查了我的html

HTML field have value

我的 view.py

    def get(self, request, slug):
       order = get_object_or_404(Order, order_number=slug)
       form = DirectorForm(instance=order)
       return render(request, 'edit_order.html', context={'form': form})

我的 forms.py

    widgets = {'order_date': forms.DateInput(attrs={'type': 'date', 'class': 'form-control'})}

2 个答案:

答案 0 :(得分:0)

好吧,您不需要datefield小部件,您必须在models.py中导入datetime模块,并使用它来填充日期或时间

答案 1 :(得分:0)

在这里更具体一点是我到我刚开始时做的一个简单django项目的github链接:http://www.djangonotes.tk 该网站是:enter image description here

您只需在您的模型中添加“ date = models.DateField(auto_now_add = True,null = True)”即可。

像这样

models.py

Bitmap bitmap;
Image image;
foreach (string imgFile in Directory.EnumerateFiles(@"D:\test\ISIC_2020_Training_JPEG"))
{
    using (Stream bmpStream = File.Open(imgFile, FileMode.Open))
    {
        image = Image.FromStream(bmpStream);
        if (ImageFormat.Jpeg.Equals(image.RawFormat)) // Check it's the correct format
        {
            bitmap = new Bitmap(image);
        }
    }
}

将数据保存到数据库时,该值将自动添加

这是一个示例:

home.html

from django.db import models
from django.contrib.auth.models import User

class note(models.Model):
    title = models.CharField(max_length=512, null=True)
    note = models.TextField(null=True)
    date = models.DateField(auto_now_add=True, null=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

forms.py

{% if notes %}
{% for note1 in notes %}
<div class="container">
    <div class="row justify-content-center">
        <div class="col">
            <div class="jumbotron">
                <h1 class="display-4">{{note1.title}}</h1>
                <p class="lead">{{ note1.date }}</p>
                <hr class="my-4">
                <p class="lead">{{ note1.note }}</p>
                <a class="btn btn-outline-info" href="{% url 'view_note'  pk=note1.id %}">Open</a>
              </div>
        </div>
    </div>
</div>

如果我听起来像一个完整的idoit,对不起,我很不擅长解释,对编程我有点陌生

谢谢, 阿育:)