Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在DJANGO中使用EmailMultiAlternations消除csrf验证?_Django_Django Views_Csrf_Django Csrf - Fatal编程技术网

如何在DJANGO中使用EmailMultiAlternations消除csrf验证?

如何在DJANGO中使用EmailMultiAlternations消除csrf验证?,django,django-views,csrf,django-csrf,Django,Django Views,Csrf,Django Csrf,我想向某个地址发送一封html电子邮件 这是我代码的一部分: 视图.py def addNewEvent(request): try: eventStatus = EventStatus.objects.filter(event=request.GET["id"])[0] #try to send the mail html = get_template('mail.html') d = Context({ 'use

我想向某个地址发送一封html电子邮件

这是我代码的一部分:

视图.py

def addNewEvent(request):   
    try:
        eventStatus = EventStatus.objects.filter(event=request.GET["id"])[0]

        #try to send the mail
        html = get_template('mail.html')
        d = Context({ 'username': usuario.name,'title':'Testing mail!!','eventStatusId': str(eventStatus.id)})

        html_content = html.render(d)

        msg = EmailMultiAlternatives('Testing yo','Skyforger!', 'mymail@gmail.com', [mail2@gmail.com])
        msg.attach_alternative(html_content, "text/html")
        msg.send()

        print('EventStatus id '+str(eventStatus.id))
    except Exception, e:
        print ('the error %s',(str(e)))

    response = getBaseJSON()
    response["event_id"]= eventStatus.id
    return HttpResponse(json.dumps(response), content_type="application/json")
mail.html

<html>
    <body>
        <h1>{{title}}</h1>
        <h2>Hi {{username}}</h2>
        <h3>Message to friend</h3>
        <form action="http://localhost:8000/confirmEvent" method="POST">
            {% csrf_token %}
            <input type="hidden" name="id" value="{{eventStatusId}}">
            <textarea name="message_to_friend"></textarea><br>
            <input type="submit" value="I'LL BE THERE!!">
        </form>
    </body>
</html>

{{title}}
你好{{username}
给朋友的信息
{%csrf_令牌%}

邮件发送正常,但在提交其表单时,会显示此错误:

禁止(403) CSRF验证失败。请求被中止

我找不到如何解决那个错误

我遵循了很多这样的答案:

没有成功


如何在html邮件中发送表单以避免CSRF错误。

您可以使用CSRF豁免装饰器禁用特定视图的CSRF保护

from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def someview():
不建议禁用csrf,但如果您愿意,可以尝试以下操作:)

我将render()替换为render_to_string(),并使用参数context_instance=RequestContext(request)执行此操作

def addNewEvent(request):   
        try:
            eventStatus = EventStatus.objects.filter(event=request.GET["id"])[0]

            #try to send the mail
            html = get_template('mail.html')
            d = Context()

            html_content = render_to_string('mail.html',{ 'username': usuario.name,'title':'Testing mail!!','eventStatusId': str(eventStatus.id)}, context_instance=RequestContext(request)) 
            msg = EmailMultiAlternatives('Testing yo','Skyforger!', 'mymail@gmail.com', [mail2@gmail.com])
            msg.attach_alternative(html_content, "text/html")
            msg.send()

            print('EventStatus id '+str(eventStatus.id))
        except Exception, e:
            print ('the error %s',(str(e)))

        response = getBaseJSON()
        response["event_id"]= eventStatus.id
        return HttpResponse(json.dumps(response), content_type="application/json")

我知道您已经看到了答案,但我没有看到您表单中的
{%csrf_token%}
,只是为了澄清一下,mail.html是否呈现并发送给在其邮件客户端中打开它的用户?CSRF令牌对应于当前用户,而不是将在其邮件中打开它的用户。此外,由于使用Context()呈现表单,因此很可能不包含CSRF令牌。您应该使用RequestContext:@mgnb在我的案例中我应该如何使用RequestContext?我读到它必须用作render_to_响应中的参数,但我没有使用该方法