I've occasionally read about pilots being locked out of the cockpit in flight, and was wondering:
Why might a pilot leave the cockpit during a short flight (1-2 hours)?
The cockpit of some airliners lacks a lavatory.
This is discussed in Skybrary: Flight Deck Security
Locked Door Flight Safety Issues
Physiological Need. Naturally, the need for flight crew to use toilets, access designated crew rest facilities or be supplied with food and drink, requires access through the flight deck door during flight. Procedures for door opening mean that egress and access will take longer with the result that the flight deck crew are not always able to go to the toilet at a time of their exact choice, which may cause them to be distracted and, exceptionally, might exacerbate an existing underlying medical issue in a way that would not otherwise have occurred.
Longer term, design change could also ensure that all operating flight crew should be secured on the flight-deck before the passenger boarding doors are closed prior to departure and remain secured on the flight-deck until after the passenger doors are opened at the destination. This would prevent any unlawful entry from the cabin and minimise any unlawful seizure by a single flight crew member.
To enable this, existing aircraft could be modified and new aircraft designed with all required crew facilities forward of the secure flight-deck door. On short/medium haul aircraft this would require a toilet, safe food & water storage and a reclining crew rest seat. Additionally on aircraft used for long range operations where in-flight relief is required provision of bunk/s. This is not a new concept, many long range aircraft already have toilet and crew rest facilities forward of the secure flight-deck door.
The FAA provides the following reasons for which a pilot is allowed to leave their assigned duty station (i.e. their seat in the flight deck) in 14 CFR §121.543 Flight crewmembers at controls:
(b) A required flight crewmember may leave the assigned duty station—
(1) If the crewmember's absence is necessary for the performance of duties in connection with the operation of the aircraft;
(2) If the crewmember's absence is in connection with physiological needs; or
(3) If the crewmember is taking a rest period, and relief is provided—
Case #3 generally applies only to long-haul flights where one or two relief pilots will alternate duty with the initial pilots, allowing each of them to have a rest period.
Europe's EASA has similar regulations. See CAT.OP.MPA.210 in this document (on pages 101-102).
CAT.OP.MPA.210 Crew members at stations
(a) Flight crew members
(1) During take-off and landing each flight crew member required to be on duty in the flight crew compartment shall be at the assigned station.
(2) During all other phases of flight each flight crew member required to be on duty in the flight crew compartment shall remain at the assigned station, unless absence is necessary for the performance of duties in connection with the operation or for physiological needs, provided at least one suitably qualified pilot remains at the controls of the aircraft at all times.
So, in summary, a pilot can leave the flight deck on short flights if:
1) The pilot needs to go to the bathroom.
Related question whose answers also mentions these regs: Are two people required in the cockpit at all times on large commercial aircraft?