Image uploads in Django
Django uses the Python Image Library (PIL) to manage images.
(info)
What happened when I installed PIL from scratch.
I need Dan Benjamin to write Django tutorials so I don’t have to figure out the best way to do this myself! Not that I think this was the best way. That’s my point.
REST
I keep hearing about REST and RESTful stuff in Rails, but I don’t really understand. Is it about serving different types of content (HTML, RSS, XML) depending on the device that’s requesting the data?
Proper redirect/render after a validation error occurs with adding or editing an item that’s dependent on another
Pass the ID of the model above what you’re validating (Publisher if you’re editing Title) in the URL (GET). Like so:
<%= start_form_tag :action => 'create_title', :id => @publisher.id %>
On the else for if the item doesn’t save (doesn’t pass validation), just put:
redirect_to :action => 'name_of_the_action_where_you_started'
Using redirect_to will mean that you don’t have to specify the ID of the model above what’s being validated, because it doesn’t redirect the whole page–just the part of the view that corresponds to that action. (is that right?)
The difference between “render :action” and “redirect_to :action”
Render has something to do with just rendering that part of the view without actually running the method again. Redirect actually takes you to the page and does stuff from scratch.
Is that it?
When to use and when not to use NOT NULL in MySQL / Rails Migrations
This is somewhat academic. Most times, the default (NULL) is just fine. I’ve been talking with JJ, and he has some specific preferences–that’s what this is based on.
Usually, having a database attribute default to NULL makes it easy to check to see if a field has information. If it doesn’t, it’s just NULL–you don’t have to check for an empty string or 0.
The main reason you would want to specifically set an attribute to NOT NULL would be if it’s a field that would be used for computation. Example: you can’t multiply by NULL.
Rails makes most NULL/NOT NULL issues easier.
- Rails (Ruby) handles NULL (NIL) strings as empty strings–so it doesn’t give you errors if you’re outputting results and get a NULL. It just shows up as blank.
- Rails sets blank form fields as NULL–unlike PHP which sets them as empty. This makes it easier to check if an item has been set (as mentioned above).
NULL Database Associations
NULL associations are another matter (and not really related to this issue). You should have a way to catch Rails exceptions if you have a record with a NULL association. Example: a car model (Corvette) without a make (Chevrolet) might make your application throw up. Check for it, and handle it appropriately.
Update 2007.7.5: Handle it like this if you want an optional association:
<%=h @project.client.name rescue nil %>
Or you could just check for it with an if or unless statement:
<% unless @project.client.nil? %>
Better way?
If you have a differing opinion / think this is full of crap, please leave a comment. I’m open to suggestions on this.
What is it called when you hit the back button and the browser remembers where you were scrolled to on the previous page?
There’s got to be a term for that. And Vanilla doesn’t do it right (at least pre 1.0).
Ternary Operator
Like this:
<?php
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? ‘default’ : $_POST['action'];
// The above is identical to this if/else statement
if (empty($_POST['action'])) {
$action = ‘default’;
} else {
$action = $_POST['action'];
}
?>
This kind of thing works in all “C-like” languages right? (JavaScript, PHP, etc.)
If/Then/Else statments are a type of Control Structure: Wikipedia, PHP docs.
Functional WP_Shortstat
The official wp_shortsat page gives you a version that no longer works with WordPress. Get a good one here. It’s even blue to match the rest of WP Admin.
Date formatting in Ruby
Here it is:
%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%S - Second of the minute (00..60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00..53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character
t = Time.now
t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003"
t.strftime("at %I:%M%p") #=> "at 08:56AM"
More information. Might it be better to use .to_formatted_s(:db) or .to_s(:db)? Not sure what that means.