Posts

Showing posts from April, 2013

Sending email with template using model form (also update template in the same view) in Django

Here is the technique that I use when sending email with template using Django model form. It also gives me the ability to update the model in the same view with the email form.


models.py:

Exporting data to Excel or PDF file in Django

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
Your views.py:

Debugging SMTP communication in Django

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/

Upgrading your project to Django 1.5

Yeah, that's right! Django 1.5 has been released a couple days ago with some new brilliant features. You can read the release annoucement of James Bennett by follow this link: https://www.djangoproject.com/weblog/2013/feb/26/15/.As usual, when upgrading to a new version of a platform, we should read the release notes carefully. And that is all you need: https://docs.djangoproject.com/en/1.5/releases/1.5/ .The following is my notes when upgrading my Django project to the new version of the platform:

How to run Django v1.1 project in Django v1.4 environment

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.

Notes: comment HTML code in Django Template

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.
Trinh

Asynchronously sending email using Django, Celery, Django-mailer

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:

Dynamically adding forms to a formset using jQuery

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):

Multi-Object-Edit with formsets by Kaula

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

SplitSelectDateTimeWidget

Image
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:....

Select All/None checkboxes

This's just a small tip I want to take note.
- Here is the script:  

<script>  function checkboxEvent(){  var select_ckb = document.getElementById("select-all"); var select_list = document.getElementsByClassName("select"); if(select_ckb.checked) {  for(var i=0; i<select_list.length; i++) {  select_list[i].checked=true;  };  } else {  for(var i=0; i<select_list.length; i++) {  select_list[i].checked=false;  };  }; }; </script> - HTML file:

....

<input type="checkbox" id="select-all" onchange="checkboxEvent();"> <input type="checkbox" class="select"> <input type="checkbox" class="select"> .... 


Enjoy this!
Trinh.