I want to redirect to homepage after successful logging in and I'm generating the HTML for the homepage using go HTML-template. When I login the url does change to /home
and the HTML page loads too. But it doesn't load the page variables passed from server-side.
func LoginHandler(w http.ResponseWriter, r *http.Request) {
....
PageVars := Login{
Username: username,
}
http.Redirect(w,r,"/home",302)
t, err := template.ParseFiles("static/home.html")
if err != nil {
log.Printf("template parsing error: ", err)
}
err = t.Execute(w, PageVars)
if err != nil {
log.Printf("template executing error: ", err)
}
}
my html page is as follows:
<!DOCTYPE html>
<html>
<body>
<div id="mainContainer" class="alignCenter">
<header class = "nav">
<div class = "nav-links">
<span class="headerTitle" id="headerTitle">{{.Username}}</span>
<form action="http://localhost:8080/api/logout" method="POST">
<span id="logout" onclick="document.forms[0].submit()">Logout</span>
</form>
</div>
</header>
</div>
</body>
After logging-in, the page displays {{.Username}}
in the header and not the logged-in username from server-side.
If I place the http.Redirect(w,r,"/home",302)
after template execution, the username loads but the url directs to the api call, like this http://localhost:8080/api/login
instead of http://localhost:8080/home
. I've been coding go only since two days now. What am I doing the wrong way here, please help.