Django 手动表单
Django manual forms
我正在尝试使用手动表格。当我在 forms.py 中检查字段名称和电子邮件是只读的时
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['name', 'email', 'body']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['name'].widget.attrs['readonly'] = True
self.fields['email'].widget.attrs['readonly'] = True
并且在 html
<form action="." method="post">
{% csrf_token %}
<div class="comment-area hide" id="comment-area">
<div class="mb-4">
<label for="{{ form.name.id_for_label }}" class="form-label">{{ form.name.label }}: </label>
<input class="input is-medium"
name="{{ form.name.html_name }}"
type="text" class="form-control"
id="{{ form.name.id_for_label }}"
placeholder="{{ request.user.username }}" readonly>
</div>
<div class="mb-4">
<label for="{{ form.email.id_for_label }}" class="form-label">{{ form.email.label }}: </label>
<input class="input is-medium"
name="{{ form.email.html_name }}"
type="email" class="form-control"
id="{{ form.email.id_for_label }}"
placeholder="{{ request.user.email }}" readonly>
</div>
<div class="mb-4">
<label for="{{ form.body.id_for_label }}" class="form-label">{{ form.body.label }}: </label>
<textarea class="textarea is-small"
name="{{ form.body.html_name }}"
class="form-control"
id="{{ form.body.id_for_label }}">
</textarea>
</div>
<div class="field">
<div class="control">
<button class="button is-success">Submit comment</button>
</div>
</div>
</div>
</form>
但是在没有姓名和电子邮件的情况下提交时,仍然出现缺少姓名和电子邮件的错误。
如果有人能帮忙,不胜感激
您在输入字段中使用了占位符,当使用 POST 时,这些值不在请求中。您可以使用 value="{{ value }}"
来设置它们。
我正在尝试使用手动表格。当我在 forms.py 中检查字段名称和电子邮件是只读的时
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['name', 'email', 'body']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['name'].widget.attrs['readonly'] = True
self.fields['email'].widget.attrs['readonly'] = True
并且在 html
<form action="." method="post">
{% csrf_token %}
<div class="comment-area hide" id="comment-area">
<div class="mb-4">
<label for="{{ form.name.id_for_label }}" class="form-label">{{ form.name.label }}: </label>
<input class="input is-medium"
name="{{ form.name.html_name }}"
type="text" class="form-control"
id="{{ form.name.id_for_label }}"
placeholder="{{ request.user.username }}" readonly>
</div>
<div class="mb-4">
<label for="{{ form.email.id_for_label }}" class="form-label">{{ form.email.label }}: </label>
<input class="input is-medium"
name="{{ form.email.html_name }}"
type="email" class="form-control"
id="{{ form.email.id_for_label }}"
placeholder="{{ request.user.email }}" readonly>
</div>
<div class="mb-4">
<label for="{{ form.body.id_for_label }}" class="form-label">{{ form.body.label }}: </label>
<textarea class="textarea is-small"
name="{{ form.body.html_name }}"
class="form-control"
id="{{ form.body.id_for_label }}">
</textarea>
</div>
<div class="field">
<div class="control">
<button class="button is-success">Submit comment</button>
</div>
</div>
</div>
</form>
但是在没有姓名和电子邮件的情况下提交时,仍然出现缺少姓名和电子邮件的错误。 如果有人能帮忙,不胜感激
您在输入字段中使用了占位符,当使用 POST 时,这些值不在请求中。您可以使用 value="{{ value }}"
来设置它们。