Reputation: 53
How can I input values from an HTML form in django. I am new to django and trying to make a clone of social network. I am not sure what code to write for forms.py,views.py, urls.py and models.py in order to input data. Also, I have various fields in html form like textbox, check box , radio buttons. How to input data from these fields to the backend ?
html
<form method = 'POST' id = "new_post" action = "/posts/new/">
{% csrf_token %}
<div class="form-group col-md-6">
<label>Title</label><br>
<input type="text" class="form-control" id="inputTitle" placeholder="Enter title">
</div>
<div class="form-group col-md-12">
<label>Situation Description</label><br>
<textarea rows="4" cols="70" class="form-control" placeholder="Enter the situation description"></textarea>
</div>
<div class="form-group col-md-12">
<label>Outcome Description</label><br>
<textarea rows="4" cols="70" class="form-control" placeholder="Enter the outcome description"></textarea>
</div>
<div class="form-group col-md-4">
<label>Outcome Type</label><br>
<select id="inputO_type" class="form-control">
<option>Positive</option>
<option>Neutral</option>
<option>Negative</option>
</select>
</div>
<br>
<div class="form-group col-md-4">
<div class="multiselect">
<div class="selectBox" onclick="showCheckboxes()">
<option>reviewed?</option><select></select>
<div class="overSelect"></div>
</div>
<div id="checkboxes">
<label for="one">
<input type="checkbox" id="one" />Commissioning</label>
<label for="two">
<input type="checkbox" id="two" />Construction</label>
</div>
</div></div>
<div class="form-row">
<br>
<div class="form-group col-md-4">
<label>Tags</label><br>
<select id="inputTag" class="form-control">
<option selected>Algorithms</option>
<option selected>Architecture</option>
<option selected>Automobile</option>
<option>...</option>
</select>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<form method="post" action="#">
<div>
<input type="checkbox" name="FormStuff" id="FormStuff" required>
<label for="FormStuff">Can't find your tag?</label>
<div class="reveal-if-active">
<label for="which-tag">Please fill in your desired tag below</label><br>
<input type="text" id="which-tag" name="which-tag" class="require-if-active" data-require-pair="#choice-tags-typetags">
</div>
<input type="submit" value="Add Tag">
<form method="post" action="#">
</div>
</div>
</div>
<br>
<div class="form-group col-md-4">
<label>Does this warrant a process improvement action?</label>
<div class="form-check">
<input type = "radio" class="form-check-input" value="" class="form-control">
<label>Yes</label>
</div>
<div class="form-check">
<input type = "radio" class="form-check-input" value="" class="form-control">
<label>No</label>
</div>
</div>
</div>
Upvotes: 0
Views: 12236
Reputation: 8525
As some people suggested above, you need to learn more about Django, the documentation is very understandable. It's possible to let Django renders the form, Django Link
To answer your question, you can simply add name
attribute to the tag, and you will be able to retrieve it in the backend with request.POST.get('name')
for post request, and request.GET.get('name')
for get request. for instance
<form action='url' method='post'>{% csrf_token %}
<textarea name='description' rows="4" cols="70" class="form-control" placeholder="Enter the situation description"></textarea>
<select id="inputO_type" name='inputO' class="form-control">
<option>Positive</option>
<option>Negative</option>
</select>
</form>
In the backend:
if request.method == 'POST':
description = request.POST.get('description')
inputO = request.POST.get('inputO')
Upvotes: 2
Reputation: 251
If you look at the Django documentation they have a specific section for building forms using Django: https://docs.djangoproject.com/en/2.0/topics/forms/
In the simplest context your form class will look something like this:
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
If you read through the rest of the django documentation they have step by step instruction on how to create a form using HTML and django.
Upvotes: 1