【问题标题】:Is there any attribute or something like placeholder in django, where I can write the permanent textdjango中是否有任何属性或类似占位符的东西,我可以在其中编写永久文本
【发布时间】:2021-07-28 11:58:45
【问题描述】:

我正在制作一个简单的表格。我正在搜索 django 中的任何术语,例如占位符,但是当用户在文本框中键入内容时,“占位符文本”会消失。我想要 django-text-field 中的“永久文本”之类的东西,当用户打开表单时,他们必须在我在编码时输入的“永久文本”之后开始写一些东西。

永久文本应保留在用户输入中,而不是像占位符文本那样消失。

【问题讨论】:

  • 初始值还是默认值?

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


【解决方案1】:

是的,你可以。 在您的form.py 中,您可以使用widget 添加所有css 属性。

class OrganizationForm(forms.Form):

    class Meta:
        model = Organization
        fields = ('org_about', 'org_pic')

    org_about = forms.CharField(
        label='About Organization',
        required=False,
        widget = forms.Textarea(
            attrs={
                'class': 'form-control valid',
                'onfocus': 'this.placeholder = ''',
                'onblur': "this.placeholder = 'About Your organization'",
                'type':'text',
                'placeholder': 'About Your organization',
                'rows': 5,
                'cols': 15,
                'value': 'about organization',

            }
        )
    )

查看here关于widgets的更多详细信息。

据我所知,没有办法完全符合您的要求。我正在分享一个想法,您可以通过这种方式实现:

input {
  background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="40" height="30"><text x="5" y="19" style="font: bold 16px Arial;">Age:</text></svg>') no-repeat;
  border: 1px solid #555;
  box-sizing: border-box;
  font: 16px "Arial";
  height: 30px;
  padding-left: 50px;
  width: 300px;
}
&lt;input type="text" /&gt;
more details 此外,您可以使用:before :after css 效果来实现您想要的。

【讨论】:

  • 感谢您的帮助,但我的问题还没有解决,我尝试了“价值”属性,它完成了一半的工作,即在文本框中显示“关于组织”文本,但它是可编辑的想要固定不变且无法更改的文本。显然,用户可以在这些固定词之后添加他们的文本。
猜你喜欢
  • 2018-04-13
  • 1970-01-01
  • 2013-06-15
  • 2017-12-08
  • 2013-08-08
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
  • 2012-11-30
相关资源
最近更新 更多