Reputation: 1
In HTML
<form method='post'>
{% csrf_token %}
{{form.name}}
{{form.email}}
<textarea name='message'>{{form.message}}</textarea>
<button type='submit'>Send</button>
</form>
How I can get the message data from my textarea in my view? Or the right way is put {{form.as_p}} in form? Please help
Upvotes: 0
Views: 11930
Reputation: 153
Above answer is perfectly alright if you want this on view. Another safe method is request.POST.get('message')
it will return None
instead of error message, if it's available.
But, you want it on template then you can use
{{ form.data.message }}
Upvotes: 1
Reputation: 11
request.POST
is basically a dictionary returned. It contains csrfmiddlewaretoken
and all form data with name specified as key in the request.POST
dict.
So, as per your form, you can get the message data from textarea by simply writing
message_data = request.POST['message']
in view.py
.
If you want to display form in your style then do it manually. Otherwise, django provides few techniques to render form, and they are as follows:
{{ form.as_table }}
will render form as table cells wrapped in <tr>
tags,
{{ form.as_p }}
will render form wrapped in <p>
tags,
{{ form.as_ul }}
will render form wrapped in <li>
tags.
Now, it depends upon you, how you want your form to look on page.
Upvotes: 0