I want to handle the errors of a POST request and re-render the form with the errors displayed above it, but the only solution to handling errors I see is http.Error() but this returns a plaintext response, not an HTML page. Is there a way to executeTemplate() and re-render the html page with the form? Am I supposed to redirect the user to the same page? If so, how do I pass the error information to that redirected page?
Edit: So, when I use this code, and try to executeTemplate, the Post request returns a 200 status code (which is wrong) and it re-renders blank page, not the template I specified.
func PostSignup(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if r.Method != http.MethodPost {
http.Error(w, http.StatusText(405), http.StatusMethodNotAllowed)
return
}
usr := users.User{}
usr.Username = r.FormValue("username")
usr.Email = r.FormValue("email")
usr.Hash = r.FormValue("password")
errors := CredErrors{}
errors.Error = "Username cannot be blank"
if usr.Username == "" {
// http.Error(w, "Username cannot be blank.", 400)
tpl.ExecuteTemplate(w, "signup.gothml", errors)
return
}