model.py
class Client(models.Model):
name = models.CharField(max_length=50)
phone_no = models.CharField(max_length=12)
mail = models.EmailField()
view.py
def create(request):
if request.POST:
form = ClientForm(request.POST)
if form.is_valid():
form.save()
subject = form.cleaned_data['name']
mail = form.cleaned_data['mail']
return HttpResponseRedirect('/')
else:
form = ClientForm()
args = {}
args.update(csrf(request))
args['form'] = form
return render_to_response('save.html', args)
html
<form action="/create/" method="post">{% csrf_token %}
<ul>
{{ form}}
</ul>
<input type="submit" name="submit" value="Create Client">
</form>
</body>
</html>
这里我成功创建了一个表单并将数据保存到db。但我也想发邮件给客户。如何直接从表单中获取电子邮件
答案 0 :(得分:1)
在您的视图代码中,您已经获得了表单数据,mail = form.cleaned_data['mail']
为您提供了电子邮件地址(如果您执行打印form.cleaned_data,您将在控制台中看到所有表单数据。