If the pilot cabin loses pressure, can the pressure difference lock the cockpit door?
Firstly: Flightdeck doors are to the best of my knowledge not hermetically sealed. 

Secondly: Doors will normally have one of several solutions employed to safely handle explosive decompression:


Some doors have panels that pop out to equalise the pressure, especially inwards.
Some doors unlock automatically in one or both directions.


Needless to say, you need one solution in either direction. Whatever pressure difference there is it will most probably very quickly be equalised and there should not be any difficulty in opening the door if need be.