如何将占位符添加到 Django 中的文本区域
How to add placeholders to Textareas in Django
我已经搜索过stack overflow,但找不到这个问题的答案。
我想要做的是向 textarea
元素添加一个占位符,但我不知道如何完成此操作。
forms.py
from django import forms
from .models import Post
from django.contrib.auth.models import User
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = (
'price',
'title',
'body',
'contact_email',
'contact_number',
'picture',
)
widgets = {
'price': forms.TextInput(attrs={'placeholder': 'Price'}),
'title': forms.TextInput(attrs={'placeholder': 'Item for sale'}),
'body': forms.TextInput(attrs={'placeholder': 'Describe the item'}),
'contact_email': forms.TextInput(attrs={'placeholder': 'Enter Price'}),
'contact_number': forms.TextInput(attrs={'placeholder': 'Enter Price'}),
}
正如您所看到的,我已经为除 body
字段之外的所有字段设置了正确呈现的占位符,因为与其他所有字段不同,它是一个 <textarea></textarea>
元素,而不是 input
元素。
创建-post.html
{% extends 'base.html' %}
{% block head %}
<!-- {% load static %}
<link rel="stylesheet" href="{% static 'accounts/login.css' %}" type="text/css"> -->
<title>Create Post</title>
{% endblock %}
{% block body %}
<div class="container"><br>
<form method="POST" action='' enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" value="submit">Submit</button>
</form>
</div>
{% endblock %}
This is how the form renders right now. As you can see the body is not large like it's supposed to be here.
如何向 textarea
元素添加占位符以使其保持其大小。
想通了。
在小部件中将 TextInput
更改为 Textarea
'body': forms.Textarea(attrs={'placeholder': 'Describe the item'})
我确实已经尝试过了,但是我在 Area 中将 A 大写。
我已经搜索过stack overflow,但找不到这个问题的答案。
我想要做的是向 textarea
元素添加一个占位符,但我不知道如何完成此操作。
forms.py
from django import forms
from .models import Post
from django.contrib.auth.models import User
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = (
'price',
'title',
'body',
'contact_email',
'contact_number',
'picture',
)
widgets = {
'price': forms.TextInput(attrs={'placeholder': 'Price'}),
'title': forms.TextInput(attrs={'placeholder': 'Item for sale'}),
'body': forms.TextInput(attrs={'placeholder': 'Describe the item'}),
'contact_email': forms.TextInput(attrs={'placeholder': 'Enter Price'}),
'contact_number': forms.TextInput(attrs={'placeholder': 'Enter Price'}),
}
正如您所看到的,我已经为除 body
字段之外的所有字段设置了正确呈现的占位符,因为与其他所有字段不同,它是一个 <textarea></textarea>
元素,而不是 input
元素。
创建-post.html
{% extends 'base.html' %}
{% block head %}
<!-- {% load static %}
<link rel="stylesheet" href="{% static 'accounts/login.css' %}" type="text/css"> -->
<title>Create Post</title>
{% endblock %}
{% block body %}
<div class="container"><br>
<form method="POST" action='' enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" value="submit">Submit</button>
</form>
</div>
{% endblock %}
This is how the form renders right now. As you can see the body is not large like it's supposed to be here.
如何向 textarea
元素添加占位符以使其保持其大小。
想通了。
在小部件中将 TextInput
更改为 Textarea
'body': forms.Textarea(attrs={'placeholder': 'Describe the item'})
我确实已经尝试过了,但是我在 Area 中将 A 大写。