オプションの添付ファイルがある場合に電子メールを送信するフォームを作成中です。djangoでオプションの添付ファイルを使用して電子メールを送信
ファイルを添付せずにメールを送信しようとします。 {}
任意のアイデア何私が間違ってやっている: 私はこのエラーMultiValueDictでは見られない
キー「ファイル」を得ましたか。あなたがファイルを送信しない場合は、当社のサーバー
forms.py
class jopcion(forms.Form):
subject = forms.CharField(max_length=100)
thecontent = forms.CharField(widget=forms.Textarea)
file = forms.FileField(widget= forms.FileInput (attrs={'name': 'file'}),required=False)
views.py
def novo(request, template_name='mailme.html'):
if request.method == "POST":
formulario = jopcion(request.POST or None, request.FILES or None)
if formulario.is_valid():
subject = request.POST['subject']
message = request.POST['thecontent']
attach = request.FILES['file']
destination = '[email protected]'
html_content = (subject,message,attach)
msg = EmailMultiAlternatives('Customer email address', html_content, '[email protected]', [destination])
msg.attach(attach.name, attach.read(), attach.content_type)
msg.attach_alternative(html_content, 'text/html')#definimos el contenido como html
msg.send() #enviar en correo
return render_to_response('done.html', context_instance=RequestContext(request))
else:
formulario = jopcion()
ctx = {'form': formulario, 'text_dc': file}
return render_to_response(template_name, ctx , context_instance = RequestContext(request))
FILES ['file'] 'がフェッチして電子メールに添付しようとする前にコードがチェックしていません。オプションの場合は、まずそれを添付しようとする前に存在するかどうかをチェックする必要があります。 – solarissmoke