python - 如何在django表单的输入框中设置值示例
问题描述
我想做完全相同的事情,唯一的区别是用户输入文本时默认文本消失
name = models.CharField(max_length=16, default="default value")
例子:
我正在为我的表单使用这种方法
class Device(models.Model):
phone_regex = RegexValidator(regex=r'^[0-9]{10}$', message="Error: Format 0611223344")
name = models.CharField(max_length=16, default="default value")
[....cut code....]
class DeviceForm(ModelForm):
class Meta:
model = Device
fields = ['name']
解决方案
看起来您想要在您的字段上使用占位符值。将以下代码添加到您的 Form 类。
name = forms.CharField(
label='Name',
widget=forms.TextInput(attrs={'placeholder': 'Type name here...'})
)
推荐阅读
- spring - 使用投影检索子集合的特定字段
- r - 将数据标签添加到 R 中的 Plotly Bullet Chart
- javascript - 无法在 React Native Flatlist 组件中运行 onEndReached
- php - 从php向表中插入行时如何修复我的MySQLi数据库
- javascript - 如何在静态文件中使用 django 模板语法
- javascript - 无法从响应中获取数据
- php - 在 Laravel 中从 S3 读取 csv?
- google-sheets - 使用间接从多个工作表中提取查询
- java - 如何提取 XML 块?
- dialogflow-es - dialogflow,Google 智能助理登录(包括 Google Home)