来自django.core.mail EmailMessage进入无限循环

时间:2016-11-03 05:43:46

标签: python django

我的Django项目中有一个简单的ahref链接:

<a class=" btn btn-md" href="/someurl/{{ some_id }}/">Notify User</a>

点击后,请求的网址为:

url(r'^someurl/(?P<some_id>\w+)/$', login_required(some_view), name='some_view'),

请求的视图是:

    def some_view(request,some_id):

            schObj = SomeModel.objects.get(id = some_id) 
            user = schObj.user
            usermail = user.email
            name = user.fname + " " + user.lname

            template = get_template('../templates/email_templates/success_mail.html')
            c = Context({})
            c.update({'some_id':some_id,'address':user.address})       
            c.update({"fullname":name})
            html = template.render(c)
            subject = "Successfull."
            email = EmailMessage(subject, html, to=[usermail],from_email='some_mail@gmail.com')
            email.content_subtype = "html"
            email.send()
            return HttpResponseRedirect(request.path)

我的问题是,点击按钮后,电子邮件会反复发送给用户。这会进入无限循环。如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

您正在重定向以在此视图中查看自己:

def some_view(request,some_id):

        schObj = SomeModel.objects.get(id = some_id) 
        user = schObj.user
        usermail = user.email
        name = user.fname + " " + user.lname
        ....
        ....

        return HttpResponseRedirect(request.path)

只需将其更改为除request.path

以外的其他内容