验证日期字段

问题描述 投票:0回答:1

我在django modelform小部件和jQuery datepicker中有覆盖日期格式,它给出了该字段无效的错误

class Sale_Invoice_Main_Page(forms.ModelForm):
    class Meta:
        model = SaleInvoice
        fields = '__all__'
        exclude = ['created_at','updated_at','transiction_type']
        widgets = {'description' : forms.TextInput(attrs={ 'placeholder' : 'description'}),
                   'invoice_no' : forms.TextInput(attrs={ 'readonly' : 'True'}),
                   'total_amount' : forms.TextInput(attrs={ 'readonly' : 'True'}),
                   'invoice_date' : forms.DateInput(attrs={ 'class' : "vdate" },format=('%d-%m-%Y')),
                   'due_date' : forms.DateInput(attrs={ 'readonly' : "True" },format=('%d-%m-%Y')),
                    }


class SaleInvoice(models.Model):
    customer = models.ForeignKey(Customer_data , on_delete=models.CASCADE)
    invoice_date = models.DateField(null=True,blank=True)
    invoice_no = models.PositiveIntegerField(unique=True)
    due_date = models.DateField(blank=True,null=True)
    address = models.TextField()
    total_amount = models.PositiveIntegerField(null=True,blank=True)
    description = models.TextField(null=True,blank=True)
    transiction_type = models.CharField(max_length=50,blank=True)
    author = models.CharField(max_length=30)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.address

jQuery日期选择器:

{#     Date Picker#}
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script>
        $( function() {
            $( ".vdate" ).datepicker({
                dateFormat: "dd-mm-yy"
            });
        } );
    </script>

我想找到我做错了,它给出了验证错误

javascript django bootstrap-4
1个回答
0
投票

有时本地化可能有点棘手。通常你会在你的设置中添加DATE_INPUT_FORMATS。在日期字段上输入数据意味着添加时,将接受来自此列表的格式

DATE_INPUT_FORMATS = [
    '%d-%m-%Y'
]

您的设置应该解决您的问题。但是当USE_L10N被设置为True时,这可能有点棘手,因为在这种情况下,区域设置指示的格式具有更高的优先级并将被应用。出于这个原因,我建议您不要在jQuery datepicker中硬编码日期格式,而是使用默认值并从DATE_INPUT_FORMATS获取日期格式。像这样的东西应该做的伎俩:

from django.utils import formats
# First date format in default (English) is '%Y-%m-%d', most European languages '%d.%m.%Y' etc.
date_format = formats.get_format("DATE_INPUT_FORMATS")[0]
date_format = date_format.split()[0].replace('%Y', 'YY').replace('%d', 'dd').replace('%m', 'mm')

并使用它的模板:

<script>
    $( function() {
        $( ".vdate" ).datepicker({
            dateFormat: "{{ date_format }}"
        });
    } );
</script>

这样,无论格式优先级如何,日期格式都将匹配。您最好在自己的context processor中包含日期格式。现在它将包含在所有模板的上下文中。

my_context_processor.py

from django.utils import formats

def common_context(request):
    ''' Common variables used in templates '''

    date_format = formats.get_format("DATE_INPUT_FORMATS")[0]
    date_format = date_format.split()[0].replace('%Y', 'YYYY').replace('%d', 'dd').replace('%m', 'mm')

    return {'date_format ': date_format}
© www.soinside.com 2019 - 2024. All rights reserved.