AFTER THE Supreme Court tossed out as unconstitutional the District’s ban on handguns, city officials were faced with a delicate task: how to protect the public from gun violence without running afoul of the constitutional right articulated in the court’s landmark 2008 ruling? That they managed to strike the right balance has been affirmed by a federal judge in a decision that should encourage other communities that want to enact sensible gun reform.

U.S. District Court Judge James E. Boasberg ruled last week that the city’s gun laws, some of the toughest in the nation, pass constitutional muster. The regulations, banning assault weapons and large-capacity magazines while imposing registration requirements for handguns and long guns, were enacted in response to District of Columbia v. Heller, the case that struck down the ban on handgun possession.

“The District of Columbia knows gun violence” is the sobering introduction to the judge’s ruling, which catalogued the years the District was the murder capital of the nation, the seven children wounded outside the National Zoo in 2000, the five people killed in the South Capitol Street shootings in 2010 and the 12 people shot to death at the Washington Navy Yard just last year. Against such a toll, the regulations imposed by the city pale in import. As Judge Boasberg wrote: “Asking gun owners to take a short class and pass a minor test — once — in order to wield deadly weapons fits the District’s interests in public safety and police protection.”

No doubt the ruling will be appealed, and no doubt the District’s gun laws will continue to come under attack from gun rights advocates on Capitol Hill. But we hope the precept that gun rights can be upheld while discharging the government’s obligation to public safety will prevail — not only as law but also as a matter of common sense.