如果模板中有多个按钮,则发布方法不起作用

时间:2018-06-19 15:36:18

标签: go go-templates

我注意到,如果我的html模板中有多个按钮,则无法从POST方法获取数据。我不知道为什么它不起作用,但是我确定它可以修复。 这是我的代码:

template.gohtml

{{ define "header" }}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
    <title>Pastozbior</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Pastozbiór</a>
  <!-- <a class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> -->
    <!-- <span class="navbar-toggler-icon"></span> -->
  </a>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="/">Dodaj pastę <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="/admin">Admin</a>
      </li>`
    </ul>
    <button class="btn btn-dark my-2 my-sm-0" style="margin-right: 5px;">Zaloguj się</button>
    <!-- <button class="btn btn-outline-dark my-2 my-sm-0">Utwórz konto</button> -->
  </div>
</nav>
{{ end }}

当我取消注释这2行时,问题就会显示:

<a class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">

<button class="btn btn-outline-dark my-2 my-sm-0">Utwórz konto</button>

addPost.gohtml

{{ template "header" }}
<div class="container">
  <div class="row">
    <div class="col-md-6 mt-5 mx-auto">
      <form method="POST">
          <div class="form-group">
            <label for="title">Tytuł pasty</label>
            <input type="text" class="form-control" id="title" name="title" placeholder="Tytuł">
          </div>
        <div class="form-group">
          <label for="copypasta">Treść pasty</label>
          <textarea class="form-control" id="copypasta" name="copypasta" rows="3"></textarea>
          <small class="form-text text-muted">Pasta nie pojawi się od razu publicznie. Musi być ona zatwierdzona prez administratora w celu uniknięcia spamu.</small>
        </div>
        <input type="submit" class="btn btn-primary mx-auto" value="Wyślij">
      </form>
    </div>
  </div>
</div>

{{ template "footer" }}

这是我的Golang代码的一部分:

if r.Method == "POST" {
        currentDateTime := time.Now().Format("2006-01-02 15:04:05")
        r.ParseForm()
        // this lines should print output of input
        fmt.Println(r.Form["title"][0])

所以主要问题是,如果我取消注释上面提到的这两行,Go代码中的最后一行将不显示任何内容。我不知道这是怎么了。

0 个答案:

没有答案