Golang应用程序中不会呈现第二个HTML模板

时间:2019-12-02 00:05:51

标签: javascript html go go-templates

在我的应用程序中,login.html模板可以很好地渲染,但是由于某些原因home.html无法渲染。它只是重新加载login.html。我尝试通过单击“登录”按钮(已附加将其发送到“ /home.html”的操作)来访问它,并且仅尝试访问“ http://localhost:3000/home.html”,但结果是相同的。有人可以指出正确的方向吗?

main.go

package main

import (
    "fmt"
    "log"
    "net/http"
    "text/template"

    "movieapp/db"
)

var loginTmpl, homeTmpl *template.Template

func init() {
    homeTmpl = template.Must(template.ParseFiles("templates/home.html"))
    fmt.Printf("Created template: %v\n", homeTmpl)

    loginTmpl = template.Must(template.ParseFiles("templates/login.html"))
    fmt.Printf("Created template: %v\n", loginTmpl)
}

func main() {
    db.Login()

    fs := http.FileServer(http.Dir("static"))
    http.Handle("/static/", http.StripPrefix("/static/", fs))

    http.HandleFunc("/", loginHandler)
    http.HandleFunc("/home", homeHandler)

    log.Println("listening...")
    http.ListenAndServe(":3000", nil)
}

func loginHandler(w http.ResponseWriter, r *http.Request) {
    if err := loginTmpl.ExecuteTemplate(w, "login", nil); err != nil {
        log.Printf("Failed to execute template: %+v", err)
    }
    fmt.Printf("Request Method: " + r.Method + " Request URL: " + r.URL.EscapedPath() + "\n")
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
    if err := homeTmpl.ExecuteTemplate(w, "home", nil); err != nil {
        log.Printf("Failed to execute template: %+v", err)
    }
    fmt.Printf("Request Method: " + r.Method + " Request URL: " + r.URL.EscapedPath() + "\n")
}

home.html

{{define "home"}}
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Login Successful</title>
    </head>
</html>
{{end}}

login.html

{{define "login"}}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>MovieList</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->  
    <link rel="icon" type="image/png" href="static/images/icons/favicon.ico"/>
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="static/vendor/bootstrap/css/bootstrap.min.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="static/fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="static/fonts/Linearicons-Free-v1.0.0/icon-font.min.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="static/vendor/animate/animate.css">
<!--===============================================================================================-->  
    <link rel="stylesheet" type="text/css" href="static/vendor/css-hamburgers/hamburgers.min.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="static/vendor/animsition/css/animsition.min.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="static/vendor/select2/select2.min.css">
<!--===============================================================================================-->  
    <link rel="stylesheet" type="text/css" href="static/vendor/daterangepicker/daterangepicker.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="static/css/util.css">
    <link rel="stylesheet" type="text/css" href="static/css/main.css">
<!--===============================================================================================-->
</head>
<body>

    <div class="limiter">
        <div class="container-login100">
            <div class="wrap-login100 p-l-85 p-r-85 p-t-55 p-b-55">
                <form class="login100-form validate-form flex-sb flex-w" action="/home.html">
                    <span class="login100-form-title p-b-32">
                        Account Login
                    </span>

                    <span class="txt1 p-b-11">
                        Username
                    </span>
                    <div class="wrap-input100 validate-input m-b-36" data-validate = "Username is required">
                        <input class="input100" type="text" name="username" >
                        <span class="focus-input100"></span>
                    </div>

                    <span class="txt1 p-b-11">
                        Password
                    </span>
                    <div class="wrap-input100 validate-input m-b-12" data-validate = "Password is required">
                        <span class="btn-show-pass">
                            <i class="fa fa-eye"></i>
                        </span>
                        <input class="input100" type="password" name="pass" >
                        <span class="focus-input100"></span>
                    </div>

                    <div class="flex-sb-m w-full p-b-48">
                        <div class="contact100-form-checkbox">
                            <input class="input-checkbox100" id="ckb1" type="checkbox" name="remember-me">
                            <label class="label-checkbox100" for="ckb1">
                                Remember me
                            </label>
                        </div>

                        <div>
                            <a href="#" class="txt3">
                                Forgot Password?
                            </a>
                        </div>
                    </div>

                    <div class="container-login100-form-btn">
                        <button class="login100-form-btn" action="/home.html">
                            Login
                        </button>
                    </div>

                </form>
            </div>
        </div>
    </div>


    <div id="dropDownSelect1"></div>

<!--===============================================================================================-->
    <script src="static/vendor/jquery/jquery-3.2.1.min.js"></script>
<!--===============================================================================================-->
    <script src="static/vendor/animsition/js/animsition.min.js"></script>
<!--===============================================================================================-->
    <script src="static/vendor/bootstrap/js/popper.js"></script>
    <script src="static/vendor/bootstrap/js/bootstrap.min.js"></script>
<!--===============================================================================================-->
    <script src="static/vendor/select2/select2.min.js"></script>
<!--===============================================================================================-->
    <script src="static/vendor/daterangepicker/moment.min.js"></script>
    <script src="static/vendor/daterangepicker/daterangepicker.js"></script>
<!--===============================================================================================-->
    <script src="static/vendor/countdowntime/countdowntime.js"></script>
<!--===============================================================================================-->
    <script src="static/js/main.js"></script>

</body>
</html>
{{end}}

0 个答案:

没有答案