python 实例化形式片段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 实例化形式片段相关的知识,希望对你有一定的参考价值。

SOME_CHOICES = [
        ('db-value', 'Display Value'),
        ('db-value2', 'Display Value2'),
        ('db-value3', 'Display Value3'),
    ]

INTS_CHOICES = [tuple([x,x]) for x in range(0, 102)]

YEARS = [x for x in range(1980, 2031)]

class TestForm(forms.Form):
    date_field = forms.DateField(initial="2010-11-20", widget=forms.SelectDateWidget(years=YEARS))
    some_text = forms.CharField(label='Text', widget=forms.Textarea(attrs={"rows": 4, "cols": 10}))
    choices = forms.CharField(label='Text', widget=forms.Select(choices=SOME_CHOICES))
    boolean = forms.BooleanField()
    integer = forms.IntegerField(initial=101, widget=forms.Select(choices=INTS_CHOICES))
    email = forms.EmailField(min_length=10)

    def __init__(self, user=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # 根据条件初始化表单数据
        # if request.method == "POST":
        #     form = TestForm(data=request.POST)
        # elif request.method == "GET":
        #     form = TestForm(user=request.user)
        #     print(request.GET)
        if user:
            self.fields["some_text"].initial = user.username

    def clean_integer(self, *args, **kwargs):
        integer = self.cleaned_data.get("integer")
        if integer < 10:
            raise forms.ValidationError("The integer must be greater than 10")
        return integer # 这里返回的是最终结果,会覆盖其它结果

以上是关于python 实例化形式片段的主要内容,如果未能解决你的问题,请参考以下文章

多线程Task

stack函数怎么用嘞?↓↓↓

loadrunner录制使用的默认浏览器是

软考 系统架构设计师 简明教程 | 软件系统建模

3声明式API及指令式命令创建namespacepodservice

ShapeRenderer使用LibGDX生成像素化形状