Bonds are traditionally valued using the discount curve, the credit spread to determine the probability of default and usually you still have to adjust the price using a spread because bonds can become illiquid.
If you ignore the last spread the price is sum of discounted cashflows except that at every point of time you need to compute the implied probability of default. If the bond defaults, the market assumes that you receive a recovery. By computing the value of the coupon leg conditionally on survival and the default leg for the default case, you should obtain a NPV which is much closer to the traded price of this security.
Usually you still need to adjust this price by a spread as the market can trade the security at a different price than your theoretical price and this is especially true for illiquid bonds. You could generalise this by saying that the market tries to anticipate what is the distribution of the expected cashflows and asks for a premium for uncertainty.
Assuming a flat rate, the basic NPV of the cashflows is:
$$
B = P_T \frac{1}{(1 + r)^T} + \sum C_t \frac{1}{(1 + r)^t}
$$
Some would try to assume that you need to add a credit spread over the discounting rate:
$$
B = P_T \frac{1}{(1 + r + c)^T} + \sum C_t \frac{1}{(1 + r +c)^t}
$$
This is still incorrect as the implied default probability from the CDS spread. You would want to compute:
$$
B = P_T \frac{1}{(1 + r)^T)}surv(T) + \sum C_t \frac{1}{(1 + r +c)^t}surv(t)
$$
where $surv(t)$ is a function the cumulative probability of survival at time t.
But you are still incorrect, the bonds actually pay you a recovery in the form of cash or new bonds in the case of default. Assuming that this recovery is represented by an amount $R$ and the instantaneous default probability at time t is represented by a function $q(t)$:
$$
B = P_T \frac{1}{(1 + r)^T)}surv(T) + \sum C_t \frac{1}{(1 + r)^t}surv(t) + R \int_{t=0}^T \frac{1}{(1 + r)^t} \cdot q(t)
$$
Even with all these adjustments, you will still not be matching the market price of a security, you can consider the difference is likely to be due to some liquidity issues or error in your estimation of your recovery or other parameters.
There are various basis methods which will yield different results, the best is to find which one the people who trade this market seems to be using and try to match the market in addition to calculate a fair theoretical value.
HTH