Search

Showing posts with label Django. Show all posts
Showing posts with label Django. Show all posts

31 May, 2019

Send email with excel file as attachment

We want to send a mail which will have some content in it's body and an attachment of excel file. The excel file is not physically saved on anywhere. We have just created it, filled it and and now , we want to send it in an email.

So what we really have as an attachment is a workbook object.

Let's get started.


    from io import BytesIO
    output = BytesIO()
    attachment.save(output)
    msg = EmailMultiAlternatives(subject, "", from_email, to_email, cc=cc, bcc=bcc)
    msg.attach('my_file.xlsx', output.getvalue(), 'application/vnd.ms-excel')
    msg.attach_alternative(content, "text/html")
    try:
        msg.send()
        success = True
    except socket.gaierror:
        success = False
    return success



10 May, 2019

Django: Creating PDF , writing content and returning as a downloaded file

Hi guys,

It took me hours to make it finally work but working with PDFs is a pain. I want to share what worked for me.

The canvas should be thought of as a sheet of white paper with points on the sheet identified using Cartesian (X,Y) coordinates which by default have the (0,0) origin point at the lower left corner of the page.


from reportlab.pdfgen import canvas
import io

pdf_buffer = io.BytesIO()
p = canvas.Canvas(pdf_buffer)
p.drawString(0, 100, "Hello People")
p.showPage()
p.save()

response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="some_file.pdf"'
pdf_value = pdf_buffer.getvalue()
response.write(pdf_value)
return response

Above code , generates a pdf with the words "Hello People" written in it. 

04 December, 2018

Production server launch using Django and Apache and mod wsgi

Some of us develop Django applications which can be used inside Intranet for various purposes.
Some ideas are:
  • HRMS apps
  • Interview Management Tools
  • Grievance Management Tool
  • Financial Report Generators. 

These apps running on intranet, generally don't face too much traffic at a time, but they do need to compute or face heavy queries which is why, it's a good idea to release them on a proper environment . Typically they are light on UI. I choose to use bootstrap which is enough to achieve a decent look . 
Still you should serve them using something which is good at serving static content. So I chose Apache. Lets get started. 

31 October, 2018

Migrating your Django Project database from SQlite3 to MySQL

Django comes with an in-build default setup for SQLLite3 which is good for learning and minor static page based websites. Like blogs which is a standard example . But as soon you decide to create projects that might entail multiple users performing different operations at the same time, SQLLite is not recommended.

Hence the decision to migrate to a better DB system must be as early as possible. I had to under go a similar experience recently . It look a lot of time, trials to finally get it working . I am going to share you the complete steps assuming you don't even have a MySQL setup .