Author: farhan

Adding * to required fields

Django’s forms (previously knows as newforms) framework has a lot cool functionality including the ability to output label tags. So, instead of typing the complete label and setting the correct for, you can just do form.field_name.label_tag and everything else is taken care of for you. The only problem is that there is no easy, documented way of adding an indicator (e.g. *) for required fields. One way would be to do if’s on each field and manually add an asterisk in the template, but that’s not DRY. After some search a few weeks ago I found an interesting post by Adil Saleem that showed one way of easily accomplishing this without changing all of your templates.

I really liked Adil’s solution, but I also wanted to change the class on the label so I could make those fields stand out (through CSS), and I also realized that he was really creating a python decorator, but in a weird way. So, here is a revised version of his solution with a slightly different approach (overriding label_tag instead of __init__) that adds the required field indicator and specifies a special CSS class (“required”) through a python decorator.

First we define two functions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from django.utils.html import escape

def add_required_label_tag(original_function):
  """Adds the 'required' CSS class and an asterisks to required field labels."""
  def required_label_tag(self, contents=None, attrs=None):
    contents = contents or escape(self.label)
    if self.field.required:
      if not self.label.endswith(" *"):
        self.label += " *"
        contents += " *"
      attrs = {'class': 'required'}
    return original_function(self, contents, attrs)
  return required_label_tag

def decorate_bound_field():
  from django.forms.forms import BoundField
  BoundField.label_tag = add_required_label_tag(BoundField.label_tag)

The first function is a decorator that provides the functionality and the second function assigns this decorator to BoundField.label_tag. The second function is really a helper function that allows you to easily add this to all your form modules.

Add the above code to one of your library files (maybe project.forms.__init__.py) and then in the module that defines your forms add the following code.

1
2
from project.forms import decorate_bound_field
decorate_bound_field()

Other than this you need to define the “required” CSS class.

1
label.required { font-weight: bold; }

Refresh your page with the form and all of your required fields will have an asterisk and the labels will be bold. Credit goes to Adil Saleem for coming up with the initial version, this is just a refinement 🙂

Presenting at TechNow09 tomorrow

Tomorrow is the TechNow09 event happening in Royal Oak, MI. ShopFiber is one of the companies presenting. We will be doing a short skit about what ShopFiber is and how it can help the average consumer shopping for electronics. I will be playing the role of “Bob,” an average consumer and Jake will be playing the role of “Neo,” a gadget lover. We are hoping that this will be a very entertaining way to showcase ShopFiber. There is a plan to broadcast the event on ustream and I will post a link once the setup is finalized. I will tweet the link as soon as I find out, so follow me on twitter if you would like to watch the live stream.

I am planning on being there for most of the time after the main event so come and say hi if you have a chance.

Facebook bug?

Let’s see if I can get this across with a super blurred photo. Below is a picture of my facebook “home” page. Notice how the same “block” (picture + three comments) is repeated several times, in total it was repeated four times. Knowing how challenging some queries with joins and grouping can be, I don’t blame facebook 🙂

Notice the repeated blocks.

Change Ubuntu repository

The default Ubuntu repository is very busy and sometimes slow, especially around releases. So, one of the first things that I generally do after installing Ubuntu is change the repository to one that is generally more responsive. Synaptics has a nice features which it tests which repository might be best in your case, use that to figure out which one to use. In my case the MIT repository mostly seems to be very snappy. Below are the steps to changing the repository. Fortunately, it is all point-and-click.

1. Open Synaptic Package Manager (Ubuntu menu > System > Administration > Synaptic Package Manager).
2. Go to Settings menu, then “Repositories”

Choose "Repositories"

3. Under the first tab “Ubuntu Software” open the “Download from:” drop down and select other.

Choose "Other" from "Download from"

4. Here you have two options, first let Synaptic figure out the best server by clicking “Select Best Server” or pick one that I know generally works pretty good in US, the MIT repository (Under “United States” choose ubuntu.media.mit.edu).

Choose the MIT repository under "United States"

5. Click “Choose Server” and then close.
6. Now reload the repository and enjoy the faster download speeds 🙂

Reading and Listening for this week

Designing for Social Web

One of the changes that I am making now is to do more reading (or listening to audio books). This week’s goal is to read Josh Porter’s Designing for the Social Web. I have just started reading the book so I will report back once I am done.

Since we are talking about the “goal,” I have also started listening to The Goal by Eliyahu Goldratt. I am only on disc two and the plan is to finish it by end of next week. I have to say that this audio book is very entertaining. Depending on how the rest is, I wouldn’t mind watching a movie version of this 🙂

Case-insensitive username login in Django

I just published a post on the ShopFiber blog that might be of interest to my readers here, it’s called Case-insensitive username login in Django. Check it out.

SL500 of Laptops?

SL 500, the car

I just saw in a promotion email that Lenovo has a laptop called SL 500. I thought it was an interesting choice of name because there was a also a Mercedes named that (at least till 2005). The SL 500 laptop is a 15.4” widescreen, ranging from Celeron to Core 2 Duo with 256MB dedicated video card. Does it deserve to be called SL 500? Probably not 🙂

SL 500, the laptop

Site Design Changed

New, simple site design

As part of my rebranding I have redesigned my personal website. As you might have noticed a little while back, I changed my twitter name from “@thebitguru” to @farhanahmad. This redesign is the next step as I become Farhan Ahmad from thebitguru 🙂 Why? you ask. Well, it is a little weird when you go to events and people recognize thebitguru, but don’t know who “Farhan Ahmad” is. I will still keep the domain name, but my name will be more prominent from now on.

For now I went for a simple design, no fancy graphics. My goal is to make minor changes as I get more time. Also, I am now publishing my twitter updates on the site.

Hopefully you like what you see 🙂 Enjoy!

Computer not waking up from hibernation?

Device Manager

I have a Linux server running all the time so I prefer to turn off my main desktop when I am not using it. Instead what I do is SSH to the server and have it send a WOL packet to wake up the desktop. Previously I would turn off my desktop when I was done, but lately I have been hibernating it because this way I can get working quicker. Anyways, I noticed that when the computer is hibernating it wouldn’t wake up, but when I would shut it down then it would work perfectly. Weird. After some research I realized that Windows has its own setting for allowing a network card to wake up the computer, and this setting is disabled by default.

Device Properties showing the checkbox (this should be checked)

To allow a card to wake up the computer you have to enable this in the network card settings. To do this go to the device manager: Start Menu > Search for “Device Manager.” In Device Manager right click on your network card under “Network Adapters” and click properties. If your network card has power management features then you should see “Power Management” tab at the top of the properties window. Go to that tab and check “Allow this device to wake the computer.”

From now on you should be able to wake up your computer using the WOL feature even when it is hibernating.

FOWA is almost here!

FOWA

Future of Web Apps – Miami 09 is almost here. Both Jake Lumetta and I are planning on attending. In addition to the conference, we decided to attend the Miami barcamp on Sunday February 22nd. So, let me know if you will be at either of these events. You can also follow me on twitter (@farhanahmad).

After the cold temperatures that we have lately had (<30, generally around 20s) in Madison (WI) I am really looking forward to the warmer Miami weather (65! :))