本文介绍了django-render_to_string不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手。但是,如果没有愚蠢的问题……这是我的。为什么我的电子邮件(在正文部分)不包含该消息?



这是我的脆皮代码:

  message = render_to_string('contact_template.txt',{'contact_name':contact_name,'contact_email':contact_email,'form_content':content},context_instance = RequestContext(request))
电子邮件= EmailMessage(提交新的联系表单,消息, [email protected] +,['[email protected]'],标题= {'Reply-To':contact_email})
email.send()

我真的很感谢您的帮助。从上午9点开始,还是一无所有。



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

 联系人姓名:


电子邮件:


内容:

和我的看法:(我不得不说这是令人生畏的...)

 从polls.forms导入ContactForm 
从django.core.mail导入来自django.template的EmailMessage
导入上下文,模板,RequestContext来自django.shortcuts导入的渲染
源自django.shortcuts导入的重定向
来自django.core.mail的导入send_mail,BadHeaderError $来自django.http的b $ b导入HttpResponse,HttpResponseRedirect来自django.template.loader的
导入render_to_string,get_template

def index(request):
返回HttpResponse( Hello,world 。您在民意测验索引中。)

def contact(request):
form_class = ContactForm

#逻辑!如果有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(新的联系表单提交,消息, [email protected] +,['[email protected]'],标题= {'Reply-To':contact_email})
email.send()
return redirect('contact')

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


解决方案

为清楚起见,我尝试将自己限制为最多字符数每行。这使得阅读行非常困难,甚至更难发现错误。


来自django.template.loader import render_to_string

context = {
'contact_name':contact_name,
'contact_email':contact_email,
'form_content':内容
}
消息= render_to_string('contact_template.txt',上下文,
context_instance = RequestContext(request))

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



  • 'contact_name'

  • 'contact_email'

  • 'form_content'


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

 联系人姓名:
{{contact_name}}

电子邮件:
{{ contact_email}}

内容:
{{form_content}}


I am a newbie. But, if there aren't stupid questions... here is mine. Why my email (in the body part) does not contain the message?

Here is my crispy code :

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, "[email protected]" +'', ['[email protected]'], headers = {'Reply-To': contact_email })
email.send()

I would really appreciate any help.. working on it from 9 a.m and still nothing..

My template (contact_template.txt), (all my received emails contained this, but no message) :

Contact Name:


Email:


Content:

and my views:(I have to say it is intimidating...)

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, "[email protected]" +'', ['[email protected]'], headers = {'Reply-To': contact_email })
            email.send()
            return redirect('contact')

    return render(request, 'contact.html', {'form': form_class,})
解决方案

For clarity purposes, I would try to limit yourself to a maximum numbers of characters per line. This makes reading the render_to_string line very hard, and makes it even harder to find errors.

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))

It seems you are missing locations in the template where the variables are to be printed. You define the following variables:

  • 'contact_name'
  • 'contact_email'
  • 'form_content'

They are however not used in the template. Example:

Contact Name:
{{ contact_name }}

Email:
{{ contact_email }}

Content:
{{ form_content }}

这篇关于django-render_to_string不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:21