Prakhar Trivedi Prakhar Trivedi - 1 month ago 20
Python Question

From django.core.mail EmailMessage Goes into infinite loop

I have a simple ahref link in my Django Project :

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


And on clicking it,the requested URL is :

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


And the requested view is :

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)


My Problem is,After clicking the button,email is getting repeatedly sent to the user.And this goes into infinite loop.How to solve this problem??

Answer

You are redirecting to view itself on this line.

return HttpResponseRedirect(request.path)

Just change it to something else, other than request.path