Web开发Django和python的新手。
有一个带有文本框的评论页面,客户可以在其中写下有关工厂访问的评论。单击提交后,它将以文本文件形式发送到Manager邮件。
我的雇主希望它更像调查页面,因此他们要求将文本框输入更改为基于表单的输入,其中客户提供了多个标签和输入,如下所示。单击提交后,应使用以下信息创建一个文本文件,然后以邮件形式发送。
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/
[]中给出的值为下拉值。
我们的网站基于Django框架构建。以下是评论页面上使用的模板文件。
{% 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)
我认为django变量'review'捕获了客户键入的整个字符串并将其保存为文件。
假设模板如下所示,
{% 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 %}
如何将表单中的所有标签/值转换为单个字符串并分配给django变量{{review}}?
谢谢
Mohan
答案 0 :(得分:2)
在Django中,应该使用Django Forms完全处理表单,因此最好在您的应用文件夹中创建一个forms.py文件,然后执行以下操作:
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
然后在您的视图函数中执行以下操作
from .forms import ReviewForm
def revfile_save(request):
form = ReviewForm(request.POST)
if form.is_valid():
review = form.cleaned_data
您甚至可以创建类似def clean_name(self)
的方法
并在输入之前检查输入是否为空或在每个字段上进行更多验证
清洁方法称为
请注意,您可以使用Django表单和ModelForms做更多的事情,因此最好阅读文档