gpt4 book ai didi

django render_to_string 缺少信息

转载 作者:行者123 更新时间:2023-12-02 14:23:22 26 4
gpt4 key购买 nike

为什么我的电子邮件(正文部分)不包含该消息?

这是我的代码:

message = render_to_string('contact_template.txt', {'contact_name':   contact_name, 'contact_email': contact_email, 'form_content': content}, context_instance=RequestContext(request))
email = EmailMessage("New contact form submission", message, "annadrybulska@gmail.com" +'', ['annadrybulska@gmail.com'], headers = {'Reply-To': contact_email })
email.send()

我的模板(contact_template.txt),(我收到的所有电子邮件都包含此内容,但没有消息):

Contact Name:


Email:


Content:

以及我的观点:(我不得不说这很吓人......)

from polls.forms import ContactForm
from django.core.mail import EmailMessage
from django.template import Context, Template, RequestContext
from django.shortcuts import render
from django.shortcuts import redirect
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.template.loader import render_to_string, get_template

def index(request):
return HttpResponse("Hello, world. You're at the poll index.")

def contact(request):
form_class = ContactForm

# logic!
if request.method == 'POST':
form = form_class(data=request.POST)

if form.is_valid():
contact_name = request.POST.get('contact_name', '')
contact_email = request.POST.get('contact_email', '')
content = request.POST.get('content', '')

message = render_to_string('contact_template.txt', {'contact_name': contact_name, 'contact_email': contact_email, 'form_content': content}, context_instance=RequestContext(request))

email = EmailMessage("New contact form submission", message, "annadrybulska@gmail.com" +'', ['annadrybulska@gmail.com'], headers = {'Reply-To': contact_email })
email.send()
return redirect('contact')

return render(request, 'contact.html', {'form': form_class,})

最佳答案

为了清楚起见,我会尝试限制每行的最大字符数。这使得阅读render_to_string线路非常困难,并且更难发现错误。

from django.template.loader import render_to_string

context = {
'contact_name': contact_name,
'contact_email': contact_email,
'form_content': content
}
message = render_to_string('contact_template.txt', context,
context_instance=RequestContext(request))

模板中似乎缺少要打印变量的位置。您定义以下变量:

  • “联系人姓名”
  • 'contact_email'
  • “表单内容”

但是,它们并未在模板中使用。示例:

Contact Name:
{{ contact_name }}

Email:
{{ contact_email }}

Content:
{{ form_content }}

关于django render_to_string 缺少信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36958543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com