Django问题:我要发送到电子邮件的发票

时间:2010-10-18 11:09:53

标签: django django-forms forms-authentication django-views sendmail

这是一个与Django相关的问题。我有一张发票,我是从显示信息的数据库创建的。现在我想知道我是否可以将这些详细信息发送到电子邮件地址。我试过在http://docs.djangoproject.com/en/dev/topics/email/查看此页面,但我不知道我是否正在寻找。我假设我也需要创建一个表单。

编辑:我想要这样的东西 - 但我想要返回整个表单。不只是主题。检查视图。对迟到的回复表示歉意。

        # urls.py
            urlpatterns = patterns('',
               (r'^index/add_invoice/$', add_invoice),
               (r'^index/invoice/$', invoice_info),
               (r'^index/invoice_details/(?P<id>\d+)/$', invoice_details),
            )

        #views.py
        @login_required
        def add_invoice(request):
         if request.method == 'POST':
          form = InvoiceForm(request.POST or None)
          if form.is_valid():
           form.save()
                                send_mail('Subject here', 'Here is the message.', 'tom@example.com', ['paul@example.com'], fail_silently=False 
   )
          return HttpResponseRedirect('/index/invoice/')
         else:
          form = InvoiceForm()
         return render_to_response('add_invoice.html', {'form': form}, context_instance=RequestContext(request))

        #add_invoice.html
        {% extends "base.html" %}

        {% block content %}
        <font face="verdana,news gothic,arial,heltevica,serif">
        <h3> Add Invoice</h3>
         <font face="verdana,news gothic,arial,heltevica,serif">
         <form method= "POST" action="">
          <div id="form">
          <table>
           {{form.as_table}}
          </table>
          <div align="center" STYLE=" margin-right:270px">
          <input type="submit" value="Submit" STYLE="background-color:#E8E8E8; color:#181818 "/>
     </div>
     </div>
     </form>
    {% endblock %}

2 个答案:

答案 0 :(得分:0)

不,您需要create a template然后需要process it,然后您需要send it

答案 1 :(得分:0)

您必须使用模板加载器加载消息模板并使用所需的变量创建上下文,然后将模板呈现为字符串。请参阅以下粗略(和不完整)示例:

from django.template import Context, loader
#More imports as needed for your code

def my_view(request):
    # Processing goes here...

    my_template = loader.get_template('invoice_template.html')
    my_context = Context({
        'purchased_items': purchased_items,
    })
    invoice_email_message = my_template.render(my_context) 

    # send the email using invoice_email_message as your message