Need A Cheatsheet For Django-South?

One of the greatest things about Django is it’s emphasis on apps and reusability and the community that follows it. My Django projects always have apps in them, and one of my most recent favorites is Django-South. The problem is that South is one of those apps that I often use once when I setup my project and then don’t touch for a while (outsite of migrations updates). So to help myself out–and hopefully others as well–I have composed a table of the commands that I use most often in South

Command Description
./manage.py schemamigration app_name –initial If you have not previously run you app through syncdb use this command to set it up with South.
./manage.py convert_to_south app_name Use you already ran your app through syncdb.
./manage.py migrate app_name –fake Use this if your like me and often use --initial when I should have used the convert_to_south command. This tells South that your database is already setup for the current schema.
./manage.py migrate Migrate the database schema for all the South enabled apps.
./manage.py migrate app_name Migrate only the specified app’s database schema
./manage.py schemamigration app_name –auto Update the schema migration for the app.

These are the commands that you will need the most, but of course South is a very well done app and you should check out the South documentation for more advanced options.

Droid X Cannot Find Position With The MyLocationOverlay Class

I have not done very much android programming, but I am getting the hang of things now. But there was one issue that came up during my most recent project where I was unable to get the MyLocationOverlay to work. It either crashed my app, didn’t find my location, or did this constant GPS lookup where I had to actually kill the app to stop it from accessing the GPS and killing my battery. I spent many hours trying to figure it out and pushed it to the end of the task list to figure out later.

When I returned to the issue, I found an article by Dimitar Darazhanski which perfectly described the issue that I was up against. As it turns out, when Motorola implemented their version of android (rant: come on people, just leave the stock OS alone), they didn’t implement the MyLocationOverlay class properly. There are reports of this having been fixed in the last update but definitely is not on my phone. This issue has also been reported to come up on a couple other Motorola phones.

To fix the issue, you can override the MyLocationOverlay class. You can find a copy of such an implementation here. When I downloaded it I had to modify a few things like image references, but it worked great. My application is now running perfectly on my Droid X.

IE8 Doubling Heights And Borders And Such

Well I had yet another problem with IE today, but this time it was only happening in Internet Explorer 8. I had a header and footer where the height was specified as 100px, but the browser was doubling it… and not just to 200px, but it was practically creating a copy, with the top border being displayed at the top and straight through the middle at 100px.

Well, in both areas I had some anchors like this:

Yeah, IE8 doesn’t like self closing tags. I have seen this issue before in IE 7 (which was working for me this time). You need to actually add a closing tag to all self-closed tags (although I think it works okay on <br />) like this:

That worked for me. Hopefully Microsoft will finally get this right in IE9.

How To Detect Length In TinyMCE

How To Detect Length In TinyMCE

When it comes down to browser WISIWYG editors, TinyMCE is one of my favorites. Don’t get me wrong, I would personally rather write is straight up HTML or reStructured, but for clients I feel this it is a great tool.

But when integrating TinyMCE, you have to worry about the length of the data because you have database limitations. So as a user is typing, you need to be able to detect not only the length of the text that the user has entered, but also the length of the HTML that TinyMCE is adding behind the scene.

Here is a code segment that I used the other to display the length to the user and then turns red if they go over the maximum length. I use jQuery in this example, but you can do whatever you want in the function.

Sending HTML Emails In Django-Registration

I have been working on a project that uses django-registration for registering our users. This allows us to make sure that they have given a valid email address, and to some extent helps to prevent bots on the system.

One of the things that we want to do is make things easy for our users, but the checked out version of django-registration only sends text emails which requires the user to manually copy and paste the registration link in their browser. Not very user friendly for users that are not as well versed in computers or mobile platforms. So I went about fixing this myself.

To do this, I found an article at StackOverflow. Essentially the answer is just to follow the standard Django method of attaching an HTML version to the text version of the email. The StackOverflow question just confirmed that there was no way to do this without modifying the source for django-registration, which is not ideal, but oh well.

So do this, modify models.py function send_activation_email:

Thank you for registering with {{ site }}.

Go to http://{{ site.domain }}{% url registration_activate activation_key %} activate your account.

Your account will expire in {{ expiration_days }} days unless you activate it.

Below you will find a zip of my new models.py file as well as the template for my activation email.

Django-CSS Custom Filter Using Slimmer

Since I started thinking about building this site, one of my goals has been to try out some optimization features that may be available. One of those features was the ability to compress CSS files. By compressing CSS files, a site will load faster because there is one less request that the browser has to make. But I wanted to still take it one step further. I wanted to remove the whitespace from the CSS as well, therefore compressing and shrinking the size of the CSS.

Originally I was going to use django-compressor, but I found that it didn’t actually compress the CSS files, but rather it just joined them. Then I found django-css, which used django-compressor but also add a filter to actually compress the code.

I know that the names are terminology are confusing but there’s nothing I can do about that. So for future reference, when I say compress I mean the compression of the code and the removing of unnecessary characters. I will use the full name when referring to django-compressor.

Upon adding the compressor app from django-css, it worked exactly like django-compressor. So I was left with implementing the filter to compress the CSS. Django-css comes with some filters for CSSTidy and YUI. I didn’t really want to mess with some of those libraries; I felt that they were a little bloated for what I wanted to do. So a quick search found python library called slimmer at http://pypi.python.org/pypi/slimmer/.

Slimmer did exactly what I wanted. It was no frills, simply removed comments, whitespace, and line breaks. So I was left with implementing the filter. I actually had a found an article elsewhere on the web that helped me get started, but I was not able to find it at the time of this writing… but thank you anonymous author.

To setup the filter, I created a new folder in the django-css folder filters and called it slimmer. Then I created the __init__.py file and put this code inside:

Then I just added this line in my settings.py to tell compressor to use the filter:

And that was that. I ran it and it worked great. All whitespace was out, all comments were out, and all the code was one line. Very efficient.

Well I hope that this was able to help. I’m sure that there is room for improvement, so leave any tips or suggestions below.

Django Sitemap

In setting up this blog I came up with the problem of setting up a sitemap. I didn’t want to use a django flatpage because I already an template and use for them, not to mention the amount of work that it would take to maintain. A quick search on Google turned up that django already has some sitemap functionality built right into the contrib! Awesome! Here is the code that was used.

First I created a new app to hold the logic for my site maps: site_sitemap. The models.py looks like this:

Then in the urls.py I put this:

And that was about it.

Hello world!

My name is Luke Jernejcic.  I work by day as an ASP.NET web developer, and after hours I mess with all sorts of various technologies ranging from Django to some RoR to the random bits of PHP.

I have setup this blog as a place where I can post my thought about things like coding, solutions to programming problems, and the random life factoids that come up.

1 4 5 6