如何 return Json django 中的数据
How to return Json data in django
我不想呈现 html 页面,而是想 return Json 来自以下代码块的数据,但我不知道如何实现它。我不想使用 rest_framework。当我使用 render
时,页面会呈现,但当我使用 JsonResponse
时,它会返回一个错误。
views.py
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.core.mail import send_mail
def contact_us(request):
if request.method == "POST":
name = request.POST.get("name")
email = request.POST.get("email")
title = request.POST.get("title")
message = request.POST.get("message")
data = {
"name": name,
"email": email,
"title": title,
"message": message
}
message = '''
New message: {}
From: {}
'''.format(data["message"], data['email'])
send_mail(data["title"], message, '', ['mine@gmail.com'])
return JsonResponse(request, 'contact_us/contact_us.html', safe=False)
contact.html
<form action="" method="POST" class="contact_us">
{% csrf_token %}
<div>
<div>Name</div>
<input type="text" name="name">
</div>
<div>
<div>Email</div>
<input type="text" name="email">
</div>
<div>
<div>Title</div>
<input type="text" name="title">
</div>
<div>
<div>Message</div>
<textarea name="message" cols="30" rows='10'></textarea>
</div>
<div>
<input type="submit", value="submit">
</div>
</form>
如果您使用 React
而不是必须从前端获取数据,则无需发送模板,您必须在 React
中创建一个包含您的联系表单的联系页面
比您必须访问视图中的所有数据
def send_mail(request):
if request.method == "POST":
name = request.POST.get("name")
email = request.POST.get("email")
title = request.POST.get("title")
message = request.POST.get("message")
data = {
"name": name,
"email": email,
"title": title,
"message": message
}
current_site = get_current_site(request)
mail_subject = 'My Subject.'
message = get_template('contact_us/contact_us.html').render(data)
to_email = request.POST.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.content_subtype = "html"
email.send()
return JsonResponse({"Success":'Check your email.'})
你必须从 django.template.loader import get_template
from django.core.mail import EmailMessage
导入这两件事
**注意:**如果您想要例外情况除外,请使用 try: except:
并使用 email.send(fail_silently=False)
以避免电子邮件错误,例如发送邮件失败
等等
查看官方文档。了解更多信息 https://docs.djangoproject.com/en/3.2/topics/email/#emailmessage-objects
我不想呈现 html 页面,而是想 return Json 来自以下代码块的数据,但我不知道如何实现它。我不想使用 rest_framework。当我使用 render
时,页面会呈现,但当我使用 JsonResponse
时,它会返回一个错误。
views.py
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.core.mail import send_mail
def contact_us(request):
if request.method == "POST":
name = request.POST.get("name")
email = request.POST.get("email")
title = request.POST.get("title")
message = request.POST.get("message")
data = {
"name": name,
"email": email,
"title": title,
"message": message
}
message = '''
New message: {}
From: {}
'''.format(data["message"], data['email'])
send_mail(data["title"], message, '', ['mine@gmail.com'])
return JsonResponse(request, 'contact_us/contact_us.html', safe=False)
contact.html
<form action="" method="POST" class="contact_us">
{% csrf_token %}
<div>
<div>Name</div>
<input type="text" name="name">
</div>
<div>
<div>Email</div>
<input type="text" name="email">
</div>
<div>
<div>Title</div>
<input type="text" name="title">
</div>
<div>
<div>Message</div>
<textarea name="message" cols="30" rows='10'></textarea>
</div>
<div>
<input type="submit", value="submit">
</div>
</form>
如果您使用 React
而不是必须从前端获取数据,则无需发送模板,您必须在 React
中创建一个包含您的联系表单的联系页面
比您必须访问视图中的所有数据
def send_mail(request):
if request.method == "POST":
name = request.POST.get("name")
email = request.POST.get("email")
title = request.POST.get("title")
message = request.POST.get("message")
data = {
"name": name,
"email": email,
"title": title,
"message": message
}
current_site = get_current_site(request)
mail_subject = 'My Subject.'
message = get_template('contact_us/contact_us.html').render(data)
to_email = request.POST.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.content_subtype = "html"
email.send()
return JsonResponse({"Success":'Check your email.'})
你必须从 django.template.loader import get_template
from django.core.mail import EmailMessage
**注意:**如果您想要例外情况除外,请使用 try: except:
并使用 email.send(fail_silently=False)
以避免电子邮件错误,例如发送邮件失败
等等
查看官方文档。了解更多信息 https://docs.djangoproject.com/en/3.2/topics/email/#emailmessage-objects