As explained below, there is a correspondence between non-zero locally principal ideals  $I$ in the order of disc. $D$ and quadratic forms $Q$ of disc. $D$.    The form $Q$ is naturally defined on  $I$ via $x \in I \mapsto N(x)/N(I).$  So $Q(x) = n$ iff $N(x) =  n  N(I)$ if $N(x I^{-1})  = n,$ so representations of $n$ by $Q$ correspond to ideals in the class
of $I^{-1}$ having norm $n$.

If we sum over all ideal classes, or equivalently over all $Q$, (and divide by the number of automorphisms of $Q$, which is the group of units in the order, to count ideals rather than elements that generate them) we will get the number of ideals of norm  $n$.  

============

The discovery of this correspondence must go back  to the 19th century, maybe to Dirichlet or Dedekind?  And Will Jagy's answer gives a very concrete description of it.

One can also give a more conceptual description of it.  Indeed,
it is fairly easy, and classical, to describe the quadratic form attached to an ideal class in conceptual terms; I explain this at the end of my answer.  There is also a conceptual approach to going from quadratic forms to ideal classes, but as far as I know it is more recent, and due to Melanie Wood (a version  of it is mentioned in [this answer](https://mathoverflow.net/a/52815/169863)).

Namely, if $Q(x,y)$ is a quadratic form over $\mathbb Z$ which is primitive, then one can consider $\mathrm{Proj} \mathbb Z[x,y]/Q(x,y)$, which turns out to be finite flat of degree $2$  over $\mathbb Z$, therefore affine, of the form $\mathrm{Spec} A$ for  some quadratic order $A$.  And in fact $A$ is isomorphic to $R[(D +\sqrt{D})/2]$, where $D$ is the discriminant of $Q$ (so $A$ is the quadratic order of discriminant $D$).    (There is no canonical identification of $A$ with this order though; we can compose with the automorphism $\sqrt{D} \mapsto -\sqrt{D}$ to get
another isomorphism.)

Now $\mathrm{Proj}$ comes with a canonical line bundle $\mathcal O(1)$,  and so this is an element of $\mathrm{Pic} A$, which can be thought of as the class group
of non-zero locally principal ideals in $A$.  (The usual class group when $D$ is a  fundamental discriminant.)

Using the isomorphism $A \cong R[(D +\sqrt{D})/2],$ we get an element of the
class group of $R[(D +\sqrt{D})/2],$ or really a pair $I,I^{-1}$ of elements,
because applying the automorphism $\sqrt{D} \mapsto -\sqrt{D}$ switches $I$
and it's inverse.

This gives a map   
$$\text{ primitive quadratic forms } Q \text{ of discriminant } D
\longrightarrow \text{ pairs } I, I^{-1} \text{  in the class group of }
R[(D +\sqrt{D})/2].$$

To see it is a bijection, one can give an explicit inverse (as I mentioned above,
this is more classical):

If $I$ is a locally principal ideal in $R[(D +\sqrt{D})/2],$ then the formula
$$x \in I \mapsto N(x)/ N(I) \in \mathbb{Z}$$
defines  a quadratic form $Q$ on the free rank two $\mathbb{Z}$-module  $I$.  (Note that $I^{-1}$ will give the same quadratic form, since formation of norms is invariant under conjugation.)