发送电子邮件与Django; CSRF令牌丢失或不正确

时间:2015-04-27 01:42:03

标签: python django email csrf

我在Django项目中遇到了一些CSRF令牌问题。 我想要做的是允许用户使用我网站上的表单将文件发送到特定的电子邮件地址。 问题是众所周知的" CSRF令牌丢失或不正确"。 我已经通过其他几个问题进行了筛选;然而,似乎我的应用程序与其他应用程序的构建方式不同。我仍然是Web开发的新手,更不用说Django了。我认为它可能与视图中的某些内容有关,但似乎这些解决方案都不适用于我的目的。 如果有人知道我们如何在这里输入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>&nbsp;</label><label>&nbsp;</label><label>&nbsp;</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。

1 个答案:

答案 0 :(得分:0)

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))