I am trying to get my head around using manual forms. When I have in my forms.py checked that fields name and email is readonly
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
And in the 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>
But still when submitting without name and email, got error on missing both name and email. If anyone can help, much appreciated
Advertisement
Answer
You used placeholder in the input fields, when using POST, those values are not in the request. You can set them by using value="{{ value }}"
instead.