jquery - 使用 Ajax 和 jQuery 的 Django 表单!无法弄清楚如何在 URL 中将控制从 Ajax 传递到 Python

标签 jquery python ajax django email

如何使用 python 文件 (project/controller/functions/email/sendemail.py) 处理使用 jQuery 从 ajax 发送电子邮件(或任何其他任务)?还是我的做法全错了?

我不想像 Django tuts 教的那样重定向到另一个页面,我只想刷新表单并仅显示成功消息。 (我的消息和表单刷新工作正常,但 Ajax 中的 URL 处理不行)。

我对此进行了多次搜索,但没有找到任何有用的东西。一些建议或示例链接将不胜感激。

我的文件与下面类似,但我使用的是 jQuery Validate,所以有点不同但原理相同,并且我的表单在我的版本上使用 bootstrap 3 进行布局。

index.html

<form method="post" action="sendemail" id="sendemail">
  <input name="name" type="text" />
  <input name="email" type="email" />
  <input name="submit" type="submit" value="send" />
</form>

main.js

$('form#sendemail').on('submit', function() {
  $.ajax({
    url: $(this).attr('action'), #(Targeting the action from the form. This is working as I can console.log the correct action as sendemail),
    type: type,
    data: data
  });
});

我尝试重定向我的 URL 以定位该文件,以便我可以使用 Ajax 中的 URL 来定位该文件,如下所示。

urls.py

from django.conf.urls import include, url
from django.contrib import admin
from . import views
from .controller.functions.email import sendemail

urlpatterns = [
  url(r'^$', views.Home, name='home'),
  url(r'^sendemail', sendemail, name='sendemail'),
  url(r'^admin/', include(admin.site.urls)),
]    

我的控制台中出现 500 服务器错误。

sendemail.py

from django.core.mail import send_mail

def sendemail(request):
  if (send_mail('Subject', 'Here is the message.', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9dfbeff2f0ddf8e5fcf0edf1f8b3fef2f0" rel="noreferrer noopener nofollow">[email protected]</a>',
['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="35415a75504d54584559501b565a58" rel="noreferrer noopener nofollow">[email protected]</a>'], fail_silently=False)):
    print(1) #Success
  else:
    print(99) #Fail

views.py

from django.shortcuts import render
from django.views.decorators.csrf import csrf_protect
from .forms import ContactForm

@csrf_protect
def Home(request):
  tpl = 'main/index.html'
  contactNotify = ""
  contactForm = request.POST


  if request.method == 'POST':
    contactForm = ContactForm(request.POST)
    if ContactForm.is_valid():
      return render(request, tpl, context)

  else:
    contactForm = ContactForm()

context = {
    'contactForm'       : contactForm
}

return render(request, tpl, context)

在 php 中,我使用 echo 返回 jQuery,因此我认为 print 与 Python 中返回值而不是 return 等效。

发送时,我在控制台中收到以下登录信息: 发布http://localhost:8888/sendemail 500(内部服务器错误)

最佳答案

您可以使用 HttpResponse 返回对 ajax 请求的响应。

import json
from django.core.mail import send_mail
from django.http import HttpResponse

def sendemail(request):
    data = {"message":"Failed"}
    if (send_mail('Subject', 'Here is the message.', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a4c5845476a4f524b475a464f04494547" rel="noreferrer noopener nofollow">[email protected]</a>',
['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="40342f002538212d302c256e232f2d" rel="noreferrer noopener nofollow">[email protected]</a>'], fail_silently=False)):
        data = {"message":"Success"}
    return HttpResponse(json.dumps(data), content_type="application/json")

如果您使用的是 Django 1.7+,则使用 JsonResponse

from django.http import JsonResponse
return JsonResponse(data)

上面的代码将返回一个json响应,您将在ajax成功函数中得到它

https://docs.djangoproject.com/en/1.9/ref/request-response/

关于jquery - 使用 Ajax 和 jQuery 的 Django 表单!无法弄清楚如何在 URL 中将控制从 Ajax 传递到 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35237037/

相关文章:

javascript - 数学游戏 JQuery/JavaScript

javascript - 仅在单击父项后打开子菜单

javascript - 跨域消费WebService

javascript - 如何让元素而不是元素漂浮在主体内部

python - 只计算一组中的男性

php - jQuery AJAX JSON 问题

java - JSF 2.2 和 Primefaces 4.0 : Refreshing image after file upload

python - 从 MongoDB 读取腌制 NumPy 数组的子集

python - Nx3 列数据到 2d 矩阵以进行图像处理

c# - 从 Pinterest 网址获取图板中的所有图像