0

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

Vadim Petrov
  • 1
  • 1
  • 1
  • 2
  • Do you mean something like this? https://stackoverflow.com/questions/2236691/display-value-of-a-django-form-field-in-a-template – shad0w_wa1k3r Feb 12 '18 at 12:24

2 Answers2

1

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 }}
0

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.