Exporting data to Excel or PDF file is one of the most common tasks in web development, especially when you have to make reports. It's quite easy these day because of the open-source world. Here are some examples how to do it:
1. To Excel file:
I will use xlwt lib, a super easy-to-manipulate python lib. I know many people will consider the build-in csv module of python instead, but after a while I found that xlwt is so convenient, so I choose it.
Check it here: https://github.com/python-excel/xlwt
After a while getting trouble with the sending email functionality of my Django app, I read the core of Django and python's smtplib module to find a way to debug the smtp connection. I only need to set the debug level = True when we create the connection in the email backend of Django (or in whatever email backend you're using):django.core.mail.backend.smtp:----------------------------------------------def open(self): ...... self.connection.set_debuglevel = True .......----------------------------------------------When I send the email from my Django app, the console will give me some useful informationRead more about the Django core email backend: https://github.com/django/django/blob/master/django/core/mail/backends/smtp.pyand the smtplib module: http://docs.python.org/2/library/smtplib.htmlIt saved my day! \m/\m/\m/
Yes, that's it!!! I know this is one of the most annoying things you've ever met when develop Django app, framework upgrading.
The first time I saw an Django application, it was Django v1.1.1. I tried to run the source code in Django 1.4.2 environment (inside virtualenv), but It becamed a mess. So, I quitted and decided to continue running the project in the shitty old v1.1 of Django.
Just a small note.
When using Django 1.1.1, do not just comment out the HTML blocks in Template, completely delete them or It will raise error (e.g template syntax error, reverse ...). Because of the template parser of Django won't ignore the commentd blocks (<!-- ... -->), so everything in there will be proceeded.
It's good to blog in a beautiful Sunday. This is the second one.
A popular situation a webapp can face is to send thoundsands of emails to the users. This action can block the system a while, and this is annoying! To solve this problem, there are many solutions. One of them is sending email to a queue and send them later in the background (automatically), so the system don't have to be freezed. Pretty cool huh?
I will setup a message queue in Django with:
Hello Sunday,Finally, I have time to blog. This is how I made forms of the inline formset can be add dynamically using jQuery.Adding this following javacsript to your template (and of-couse, the jquery lib):
A Germanian developer named Christian Kaula wrote a great tutorial about manipulating multiple objects (forms) from Django formset here . But, as he mentioned in the article, the code was written from his memory, so It's buggy. A couple days ago, I've just contacted him to ask about parts of the code I didn't understand. Luckily, he's replied and fixed the code. Thanks for that, I can apply his tutorial to my project without any doubts. I just want to take some notes here for future use.- Your forms.py
Django only provides us SelectDateWidget to render DateField in Django's Template (django.forms.extras.widgets.SelectDateWidget) . What if you have a DateTimeField and want to have a Select Date and Select Time widget in your template (not a poor input textbox)? Web design means making beautiful UIs, right?Luckily, a guy here has written a custom widget name SplitSelectDateTimeWidget to save the world! Here is how I make use of Brad's widget in my project:- Create a file name widgets.py in your app directory (e.g. myapp.widgets.py). Note that SplitDateTimeWidget also uses SelectTimeWidget which is also written by Brad:....