Times are changing
Jun 23rd
It has been almost a full year since my last post, in that short time much has changed. My previous place of work has decided to pull their offices out of town leaving myself and many others behind. Many sought refuge in a few larger businesses in town and seem to be enjoying it. A few have ventured out into smaller local companies, bringing with them there knowledge and expertise. In January I decided I would do something I have always wanted to do, I incorporated Nullobject and started working on contracts and whatever work I could find in an effort to build up some money into the company as well as keep my bills payed.
I have decided that I want to keep a record of some of the stuff I am working on, as well as continue to help people learn in all areas. I plan on posting articles on things as I did before, problems I tackle and solve, solution to problems people have, as well as the usual rants and opinions. I am hoping that I will learn more by teaching others and others will learn as well. So anyone that can bare my horrible writing and want to learn new stuff together, are welcome to join me. I plan on keeping updates frequent and with substance.
Django: Sending mail with Templates
Jul 19th
It’s interesting how only a few lines of code can save so much work. A few days ago, I was working on adding a new feature to a system I wrote a while ago using django. I noticed there were a few places that I wanted to send out an email along with logging. I started looking into Django’s ability to send out emails, after much searching I found the mail_admins function. I wasn’t a huge fan of having the email content buried in the code. I did a few quick searches on the documentation and came up with no real solutions. Using Django’s render_to_response as an example I created a function that would allow me to send an email using a Django template.
from django.template import loader
from django.core.mail import mail_admins
def render_to_email(template, subject, context):
template = loader.render_to_string(template, context)
mail_admins(subject, template)
The function works exactly like Django’s render_to_response function, with one added value to the function for setting the email’s subject
render_to_email('email_general_error.html', 'General Error', { 'request': request, 'message': message})
Website Image Caching
May 9th
In a previous post titled Django Json Woes I mentioned setting response headers for caching for Json responses. This was done to make sure that data that was sent was always refreshed and the browser didn’t use cached content. Most times this is accomplished with a technique called “cachebuster”, which means adding a pseudo random string to your request. This is done because your web browser, performs caching on your content based on urls including any parameters that are passed. By adding a random string onto the URL, your tricking your browser into thinking its a totally different URL and the browser will bypass the cache and download it.
I personally am not a huge fan of using the “cachebuster” because deciding if something should be cached or not should be decided on by the site and less by the connection to the site for 90% of requests.
This kind of cache bypassing can be useful when trying to get a bit more performance from a image heavy website. By hosting your images with a cache set to essentially never expire, and having your website pass “cachebuster” type information at the end.
<img src="/images/background.jpg?1273456709" >
The data at the end of the image url could be anything that indicates that the image has changed. When a request comes in the first time the browser would download the image based on the given url, each time afterwards it would continue to use the cache image. When you modify or change the image the data on the url would change and any hit from cached browsers would see the change and download the new copy of the image.
I am currently working on a template command for django that can do this easily, once its completed ill update this post.
Html5 LocalStorage, Client Side Memcached.
May 5th
Although HTML5 isn’t 100% finalized, there are a few things that have been finished up and implemented in the newer browsers. One such feature is a local storage, it doesn’t provide any fancy functions it’s more of a client side Memcached. It’s a persistent storage area that’s accessed using a key, value pair, attached to the window object.
window.localStorage['key'] = 'value'
Setting the value of the key is just a matter of assigning a variable. Obtaining an already saved value is also just as easy.
var value = window.localStorage['key']
Here is a small sample using a few basic functions to show how it might be used. You are able to enter text in the box and save it. when the page reloads that new value will display in the browser. When you click on load it will load whatever value is saved in the current storage.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Html LocalStorage</title>
<script type="text/javascript" charset="utf-8">
function save() {
obj = document.getElementById('btn_text');
if(obj.value)
window.localStorage['text'] = obj.value
}
function load() {
obj = document.getElementById('btn_text');
value = window.localStorage['text'];
if(value)
obj.value = window.localStorage['text'];
}
</script>
</head>
<body onload="load();">
<input type="text" value="" id="btn_text">
<input type="button" onclick="save();" value="Save"> | <input type="button" onclick="load();" value="Load">
</body>
</html>
Keep in mind that because of browsers are only starting to roll out these implementations now, it might not work in all browsers. Also be sure to use the most recent versions.
I’ll be writing and posting more HTML5 posts, giving examples and code on implementing some of the new exiciting things that will be out soon.
