Reputation: 949
New to web development Django and python.
There is a review page with text box, in which customers write their review about the factory visit. When clicked submit it sent to Manager mail as text file.
My employer wants it more like survey page so they asked to change the text box input to form based input, where customer provided with multiple labels and inputs as below. when clicked submit, a text file should be created with below info and send as mail.
Name :
Phone :
email :
precaution and safety gears given: [yes/no]
Unit 1 ambience: [Very satisfied/Satisfied/Neutral/Unsatisfied/Very unsatisfied]
Unit 2 ambience: [Very satisfied/Satisfied/Neutral/Unsatisfied/Very unsatisfied]
Unit 3 ambience: [Very satisfied/Satisfied/Neutral/Unsatisfied/Very unsatisfied]
General feedback : /text box for detailed review if any/
values given in [] are drop down values.
our website built on Django framework. Below is the template file used on review page.
{% extends 'page1.tmpl' %}
<form id="reviewform" method="post" action="/sjfacweb/revfile/save">
<pre><textarea name="review" id="review">{{ review }}</textarea></pre>
<input class="button" type="submit" name="submit" value="Save" />
</form>
{% endblock content %}
views.py
@require_POST
@csrf_protect
def revfile_save(request):
"""
This page processes and saves review file.
"""
review = request.POST.get('review', "").replace('\r\n','\n')
reviewfile_name = "/root/sjfacweb/" + remote.sjfacweb()
remote.write_reviewfile(reviewfile_name,False,review)
I think the django variable 'review' captures the entire string typed by the customer and save it as file.
Suppose if the template look like below,
{% extends 'page1.tmpl' %}
<form id="reviewform" method="post" action="/sjfacweb/revfile/save">
Name: <input type="text" name="Name" value=""><br>
Phone: <input type="text" name="Phone" value=""><br>
email: <input type="email" name="email" value=""><br>
precaution and safety gears given:
<select>
<option value="yes">yes</option>
<option value="No">No</option>
</select>
<br>
Unit 1 ambience:
<select>
<option value="Very Satisfied">Very Satisfied</option>
<option value="Satisfied">Satisfied</option>
<option value="neutral">neutral</option>
<option value="Unsatisfied">Unsatisfied</option>
<option value="Very Unsatisfied">Very Unsatisfied</option>
</select>
<pre><textarea name="review" id="review"></textarea></pre>
<input class="button" type="submit" name="submit" value="Save" />
</form>
{% endblock content %}
How to convert all the label/values in the form to a single string and assign to the django variable {{ review }}?
Thanks,
Mohan
Upvotes: 0
Views: 952
Reputation: 571
In Django it is expected to handle forms completely with Django Forms so it is better to create a forms.py file in your app folder and then do something like this:
from django.forms import forms
class ReviewForm(forms.Form):
PRECAUTIONS_CHOICES = (
('no', 'No'),
('yes', 'Yes')
)
UNIT1_CHOICES = (
('Very Satisfied', 'Very Satisfied'),
('Satisfied', 'Satisfied'),
('neutral', 'neutral'),
('Unsatisfied', 'Unsatisfied'),
('Very Unsatisfied', 'Very Unsatisfied'),
)
name = forms.Charfield()
phone = forms.Charfield()
email = forms.EmailField()
precautions = forms.ChoiceField(choices= PRECAUTIONS_CHOICES, widget=forms.Select())
unit1_ambience = forms.ChoiceField(choices= UNIT1_CHOICES, widget=forms.Select())
review = forms.CharField(widget=forms.Textarea())
def clean(self):
# after checking every field is available and then concat values to each other
if 'name' in self.cleaned_data and 'phone' in self.cleaned_data and ... :
name = self.cleaned_data.get('name')
phone = self.cleaned_data.get('phone')
...
review = self.cleaned_data.get('review')
expected_string = name + '\n' + phone + ... + review
# concat all with review and then return string
return expected_string
and then in your view function do something like this
from .forms import ReviewForm
def revfile_save(request):
form = ReviewForm(request.POST)
if form.is_valid():
review = form.cleaned_data
you can even create methods like def clean_name(self)
and check if the inputs are empty or do more validation on each field before the
Clean method is called
be aware you can do much more with django forms and ModelForms so it is better that you read the docs
Upvotes: 2