views:

49

answers:

2

Hello, 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 %}
A: 

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

Ignacio Vazquez-Abrams
In there document they said you can create in (I'm guessing views.py) in two lines from django.core.mail import send_mailsend_mail('Subject here', 'Here is the message.', '[email protected]', ['[email protected]'], fail_silently=False)but I don't know how this works. What would be the best appropriate template to write up?
Shehzad009
Whatever you feel is appropriate. http://www.google.com/search?q=sample+invoice
Ignacio Vazquez-Abrams
No, I know what an invoice is and how it works but I need to find out how to code it is such a way that there is a button that I can click to send the invoice receipt to the clients email address.
Shehzad009
You create a button that invokes a POST that calls a view that processes a template, mails the result, and returns a HTML page.
Ignacio Vazquez-Abrams
@Ignacio - I have have done something similar to that with the forms. I wonder how you can send instead of the message, the whole form. Is there a way possible?
Shehzad009
A: 

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
Evan Porter