我的Django项目中有一个简单的ahref链接:
<a class=" btn btn-md" href="/someurl/{{ some_id }}/">Notify User</a>
然后单击它,请求的URL是:
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='[email protected]')
email.content_subtype = "html"
email.send()
return HttpResponseRedirect(request.path)
我的问题是,单击按钮后,电子邮件将反复发送给用户。这将陷入无限循环。如何解决此问题?
最佳答案
您正在重定向以在此视图中查看自身:
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
以外的其他名称