gpt4 book ai didi

html - django 表单申请设计的 html 元素

转载 作者:行者123 更新时间:2023-11-28 15:42:25 25 4
gpt4 key购买 nike

我有这个 html 文件:

<form method='POST'>
{% csrf_token %}
<input name='foo'/>
<button type="submit">submit</button>

</form>

现在,想象一下这个表单背后有 css 属性,我只想要一个可以配置一些输入的表单。举个例子。该字段为必填项,最小长度为 10 个字符,等等。

我如何使用 Django 表单处理它?<​​/p>

是这样的:

from django import forms

class inputform(forms.Form):
input_line = forms.CharField(max_length=20, min_length=10, name='foo')

我如何将其应用于 vies.py 以及如何将错误输出到 html?

非常感谢您的回复。

最佳答案

你可以试试这个。

class inputForm(forms.Form):
input_line = forms.CharField(max_length=30, widget=forms.TextInput(attrs={'class' : 'input-field form-control'}))



def clean(self, *args, **kwargs):
cleaned_data = super(inputForm, self).clean()
my_input = self.cleaned_data.get("input_line")
if not my_input:
raise forms.ValidationError('This field is required')
if len(my_input) < 10:
raise forms.ValidationError('the min length is 10 character')
return cleaned_data

clean() 方法用于验证来自表单的输入。

关于html - django 表单申请设计的 html 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43282087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com