I found this:
One explanation that is often repeated is that the phrase derives from the British House of Commons. Arguments in the House are often heated. To deter members of opposing parties from attacking each other, two parallel red lines are marked, two sword-lengths apart, on the floor of the house. MPs are expected to stay behind these lines when a speech is in progress. Members, of course, no longer carry swords, but the tradition remains. Visitors to the House of Commons are very likely to hear this tale related by a tour guide. Counting against this supposed derivation is the fact that the current Commons Chamber dates from only 1950, when the building was rebuilt following WWII bomb damage. Paintings of earlier Commons chambers, from the times when members might actually have worn swords, show no such lines.
And this:
It is commonly and erroneously thought that its origins lie in the British House of Commons where sword-strapped members were instructed to stand behind lines that were two sword-lengths apart from their political rivals in order to restore decorum. However, there is no record of a time when Members of Parliament were allowed to bring swords into the Chamber. Historically, only the Serjeant at Arms carries a sword as a symbol of his role in Parliament. There are loops of pink ribbon in the Members' cloakroom for MPs to hang up their swords before entering the Chamber to this very day as a result of this rule. In fact, there were not any lines in the Chamber in the days that gentlemen carried swords.
It seems the correct version is this one:
The most likely origin of the term goes back to the wooden decked ships of the Royal Navy during the late 17th or early 18th century. Barefooted seamen had to stand at attention for inspection and had to line up on deck along the seams of the wooden planks, hence to "toe the line". The first mention of this use in literature stems from a story about navy life widely published in 1831 and written by Captain Basil Hall RN. Hall served in the Royal Navy from 1802.
The first site confirms this version:
Other early examples of 'toe the ...' have a nautical connection. In the 19th century, sailors were expected to prepare themselves for group punishment by standing in formation on deck and 'toeing the line' between boards - also called 'toeing the crack'. This usage is the earliest that I've found for 'toe the line' in print - from The Edinburgh Literary Journal, January - June 1831:
"The matter, therefore, necessarily became rather serious; and the whole gang of us being sent for on the quarter-deck, we were ranged in a line, each with his toes at the edge of a plank, according to the orthodox fashion of these gregarious scoldings, technically called toe-the-line matches."
Oh well, Peter's story may not be correct, but researching it led to learning about the pink ribbons, which I find very interesting indeed.
Under the best vaulted ceilings neo-Gothic architecture has to offer, the old is in constant friction with the new. MPs are provided with Dell laptops and BlackBerry Curves, but in the House of Commons cloakroom, they are also allocated their own coat hangers, each of which has a pink ribbon — for hanging up their swords.
How quaint...
On the other hand, they have party whips:
A whip is an official in a political party whose primary purpose is to ensure party discipline in a legislature. Whips are a party's "enforcers", who typically offer inducements and threaten punishments for party members to ensure that they vote according to the official party policy.
That's one way to ensure the MPs toe the line!