用 Django 发送电子邮件; CSRF 令牌丢失或不正确
Sending an email with Django; CSRF token missing or incorrect
我的 Django 项目中的 CSRF 令牌有一些问题。
我想要做的是允许用户使用我网站上的表格将文件发送到特定的电子邮件地址。
问题是众所周知的 "CSRF token missing or incorrect"。
我筛选了其他几个问题;但是,似乎我的应用程序的构建方式与其他应用程序不同。我对 Web 开发还是很陌生,更不用说 Django 了。我认为这可能与视图中的某些内容有关,但这些解决方案中 none 似乎对我的目的有用。
如果有人知道我们如何放入 CSRF 令牌或在此处更正,以下是为添加此电子邮件功能而修改的文件:
#views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.context_processors import csrf
from django import forms
from ReadMyPaper.feedbackEmail.forms import EmailForm
#Create your views here.
def send_email(request):
if request.method != 'POST':
form = EmailForm()
return render_to_response('email.html', {'email_form': form})
form = EmailForm(request.POST, request.FILES)
if form.is_valid():
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
email = form.cleaned_data['email']
attach = request.FILES['attach']
try:
mail = EmailMessage(subject, message, settings.EMAIL_HOST_USER, [email])
mail.attach(attach.name, attach.read(), attach.content_type)
mail.send()
#return render_to_response('Email.html', {'message': 'Sent email to %s'%email})
args = {}
args.update(csrf(request))
args['form'] = EmailForm()
#return render_to_response('email.html' , args)
#return render_to_response(
#'email.html', args
#context_instance=RequestContext(request)
return render_to_response("email.html", args)
except:
# return render_to_response('admin/Error.html', {'message': 'Either the attachment is too big or corrupt'})
return render_to_response('loggedin.html')
表格...
from django import forms
class EmailForm(forms.Form):
email = forms.EmailField()
subject = forms.CharField(max_length=100)
attach = forms.Field(widget = forms.FileInput)
message = forms.CharField(widget = forms.Textarea)
模板...
{% block content %}
{{message}}
{% if email_form %}
<form method="POST" action ="." enctype="multipart/form-data">
{% csrf_token %}
<br></br>
{{email_form.as_p}}
<label> </label><label> </label><label> </label>
<input type ="submit" name = "send" value = "Send"/>
</form>
{% endif %}
{% endblock content %}
和设置文件...
#settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# Host for sending e-mail.
EMAIL_HOST = 'smtp.gmail.com'
# Port for sending e-mail.
EMAIL_PORT = 587
# Optional SMTP authentication information for EMAIL_HOST.
EMAIL_HOST_USER = ‘myemail@gmail.com'
EMAIL_HOST_PASSWORD = ‘mypassword'
EMAIL_USE_TLS = True
显然这些不是 gmail 地址的真实电子邮件和密码。我有一个实际使用的 gmail。
将 render_to_response()
调用替换为 render()
:
from django.shortcuts import render
return render(request, 'email.html', {'email_form': form})
或者将 RequestContext
实例传递给 render_to_response()
:
return render_to_response("email.html", {'email_form': form},
context_instance=RequestContext(request))
我的 Django 项目中的 CSRF 令牌有一些问题。 我想要做的是允许用户使用我网站上的表格将文件发送到特定的电子邮件地址。 问题是众所周知的 "CSRF token missing or incorrect"。 我筛选了其他几个问题;但是,似乎我的应用程序的构建方式与其他应用程序不同。我对 Web 开发还是很陌生,更不用说 Django 了。我认为这可能与视图中的某些内容有关,但这些解决方案中 none 似乎对我的目的有用。 如果有人知道我们如何放入 CSRF 令牌或在此处更正,以下是为添加此电子邮件功能而修改的文件:
#views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.context_processors import csrf
from django import forms
from ReadMyPaper.feedbackEmail.forms import EmailForm
#Create your views here.
def send_email(request):
if request.method != 'POST':
form = EmailForm()
return render_to_response('email.html', {'email_form': form})
form = EmailForm(request.POST, request.FILES)
if form.is_valid():
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
email = form.cleaned_data['email']
attach = request.FILES['attach']
try:
mail = EmailMessage(subject, message, settings.EMAIL_HOST_USER, [email])
mail.attach(attach.name, attach.read(), attach.content_type)
mail.send()
#return render_to_response('Email.html', {'message': 'Sent email to %s'%email})
args = {}
args.update(csrf(request))
args['form'] = EmailForm()
#return render_to_response('email.html' , args)
#return render_to_response(
#'email.html', args
#context_instance=RequestContext(request)
return render_to_response("email.html", args)
except:
# return render_to_response('admin/Error.html', {'message': 'Either the attachment is too big or corrupt'})
return render_to_response('loggedin.html')
表格...
from django import forms
class EmailForm(forms.Form):
email = forms.EmailField()
subject = forms.CharField(max_length=100)
attach = forms.Field(widget = forms.FileInput)
message = forms.CharField(widget = forms.Textarea)
模板...
{% block content %}
{{message}}
{% if email_form %}
<form method="POST" action ="." enctype="multipart/form-data">
{% csrf_token %}
<br></br>
{{email_form.as_p}}
<label> </label><label> </label><label> </label>
<input type ="submit" name = "send" value = "Send"/>
</form>
{% endif %}
{% endblock content %}
和设置文件...
#settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# Host for sending e-mail.
EMAIL_HOST = 'smtp.gmail.com'
# Port for sending e-mail.
EMAIL_PORT = 587
# Optional SMTP authentication information for EMAIL_HOST.
EMAIL_HOST_USER = ‘myemail@gmail.com'
EMAIL_HOST_PASSWORD = ‘mypassword'
EMAIL_USE_TLS = True
显然这些不是 gmail 地址的真实电子邮件和密码。我有一个实际使用的 gmail。
将 render_to_response()
调用替换为 render()
:
from django.shortcuts import render
return render(request, 'email.html', {'email_form': form})
或者将 RequestContext
实例传递给 render_to_response()
:
return render_to_response("email.html", {'email_form': form},
context_instance=RequestContext(request))