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

发布于 2024-09-27 13:56:59 字数 2091 浏览 5 评论 0原文

这是一个与 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.', '[email protected]', ['[email protected]'], 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 %}

this is a Django related question. I have an invoice that I have created from a database which displays the information. Now I want to know is if I can send these details to an email address. I have tried looking at this page at http://docs.djangoproject.com/en/dev/topics/email/, but I don't know if I am looking for that. I am assuming I need to create a form maybe as well.

Edit: I want something like this - but I want to return the whole form. Not just the subject. Check the views. Apologies for the late reply.

        # 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.', '[email protected]', ['[email protected]'], 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 %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So尛奶瓶 2024-10-04 13:56:59

不,您需要创建模板,然后您需要处理它然后你需要发送

No, you need to create a template and then you need to process it and then you need to send it.

陌伤ぢ 2024-10-04 13:56:59

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

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

You have to use the template loader to load your message template and create a context with the variables you want and then render the template into string. See the following crude (and incomplete) example:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文