我是 Django 的初学者。我无法通过 ajax 代码进入视图。我已经在按钮单击时对其进行了编码。编写此代码是为了通过单击按钮发送电子邮件。在 view.py 中,虽然显示 def 中的代码错误,否则如果在文档加载时发送类电子邮件中的代码。应在单击按钮时发送电子邮件。你能帮我解决这个问题吗?
user_profile.html
<button class="btn btn-orange" id="btn_delete" onclick="delete_profile1()">Delete</button>
<script>
function delete_profile1() {
$.ajax({
type: 'POST',
url: '{% url "delete_profile" %}',
data: { },
success: function () {
toastr.info('Preference Updated Successfully')
}
});
}
</script>
urls.py
path('delete_profile', delete_profile.as_view(), name='delete_profile'),
views.py
class delete_profile(View):
def post(self, request, *args, **kwargs):
print("nothing")
template = loader.get_template("frontend/subscription-start.html")
email_content = "deletion confirmation"
send_mail(
'No Dowry Marriage - Subscription',
email_content,
'developer@goodbits.in',
[abcd@gmail.com],
html_message=email_content,
fail_silently=False
)
答案 0 :(得分:0)
首先,将 [abcd@gmail.com]
修正为 ['abcd@gmail.com']
。
然后,请阅读以下文档,尝试解决ajax CSRF问题。