I'm new to Django and I am stuck at how can I use my html request form to show in view :
1. HTML form :
<form action="" method="post">
<label for="your_name">Your name: </label>
<input id="your_name" type="text" name="name" value="{{ name }}">
<input type="submit" value="OK">
</form>
2. views/formout.py
def pageform(request):
name = request.POST.get('name', '')
return render(request, 'en/public/index.html',{name : "name"})
3. URL
url (r'^form$', 'jtest.views.formout'),
4. index.html
<p>
name : {{name}}
</p>
Best Solution
First, you need to use an actual form class to sanitize and normalize any user-supplied values into their Python equivalent:
Second, you need to leverage the form in your view:
Lastly, your template needs to leverage the form:
By passing the initial value to the form class, Django will bind the value for you. If the form has errors, and
name
has a value, Django will re-bind the value submitted to that field from therequest.POST
dictionary.