President Carter's rivals for the Democratic presidential nomination broke the ranks of party unity yesterday, leveling sharp attacks at Carter's handling of the crisis in Iran and Afghanistan.

In separate statements, Sen. Edward M. Kennedy and California Gov. Edmund G. (Jerry) Brown Jr. blasted Carter for "weak" policies, with Kennedy adding that the president's decision to withhold grain shipments from the Soviet Union "won't work."

It was the harshest attack on Carter from his Democratic opponents since the crisis began in Iran Nov. 4 and signaled a clear end to a period of relative unity.

"All of us want strong action against the Soviet Union," Kennedy said in a statement. "But a grain embargo won't work, and it's unfair to farmers. The Soviet troops won't leave Afghanistan, and the American farmer will pay the price for an ineffective foreign policy.

"A weak policy can't be redeemed by suddenly becoming tough on farmers," Kennedy added. "In 1976, President Carter said repeatedly that he would never embargo grain. Now he's done it, and it could cost the taxpayers two or three billion dollars to buy up all the grain. And even that won't protect the farmers."

Kennedy said he was "deeply concerned that our foreign policy is out of control, that all we can do is react to events that constantly take us by surprise."

Brown, en route to Iowa for several days of campaigning, was broader in his criticism, calling the president "naive" and "relatively weak."

Brown said he blamed the president for not recalling U.S. diplomats from Iran before the deposed shah was admitted to this country for medical treatment. He said Carter failed to heed warnings from Iran about retaliation if the shah were admitted.

The California governor said the Iranian crisis in turn led to the Soviet invasion of Afghanistan, and he said Carter is trying to make political capital out of both crises.

"That's a very clever tactic," Brown said. "He gets us into a crisis, then he takes whatever reasonable steps can be done after we're there."

Last month, when Kennedy attacked the shah as a repressive leader, Brown rallied to support Carter. But he has been critical of the president ever since Carter announced he would not participate in the Democratic debate in Iowa that was to have been held Monday night. The debate has been canceled.

Kennedy, deploring the Soviet invasion of Afghanistan, said the United States should be reinforcing its "political, economic and military positions in the Middle East and Southwest Asia," as well as trying to bring the Soviet Union to account "in the United Nations and other forums," rather than cutting off grain to the Soviets.

"This nation needs to be firm with its foes," he said. "We can't afford to be surprised when the Soviets lie to us."

Responding to Kennedy's statement, White House press secretary Jody Powell said, "I think for Sen. Kennedy to be calling for strengthening our military posture is a new high in political hypocrisy. The senator has one of the worst voting records on national defense in the United States Senate. o

"I really think if you read his statement, he fails to suggest any action that would be punitive to the Soviet Union."

Powell said he had no response to Brown. "I often have difficulty following Gov. Brown's logic and thought processes."