beego,重新定义了页面未找到,404页面没有显示HTML,为什么?

huangapple go评论72阅读模式
英文:

beego, redefined page not found, 404 page is not show html, why?

问题

我在router.go文件中重新定义了page_not_found函数:

func page_not_found(rw http.ResponseWriter, r *http.Request) {
    t, _ := template.ParseFiles(beego.ViewsPath + "/404.html")
    data := make(map[string]interface{})

    t.Execute(rw, data)
}

init函数中使用了:

beego.Errorhandler("404", page_not_found)

当我在控制器中使用this.Abort("404")调用404页面时,404页面没有显示HTML,而是显示了页面的HTML文本,类似于这样:

beego,重新定义了页面未找到,404页面没有显示HTML,为什么?

英文:

I redefined the page_not_found on router.go,

func page_not_found(rw http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles(beego.ViewsPath + "/404.html")
	data := make(map[string]interface{})

	t.Execute(rw, data)
}

and in init function used

beego.Errorhandler("404", page_not_found)

when i called 404 by used this.Abort("404") in controller, the 404 page was not show html, it showed the page html text, liked this

beego,重新定义了页面未找到,404页面没有显示HTML,为什么?

答案1

得分: 1

因为我写了这样的404页面:

<link rel="stylesheet" href="/static/css/style.css">
<link rel="stylesheet" href="/static/css/a.css">
<div class="html404">
    <div class="title">404</div>
    <div class="link">
        <div><a href="/">回首页</a></div>
    </div>
</div>

这导致了问题。当我将HTML更改为:

<html>

<head>
     <title>Aiysea 404 Page</title><link rel="stylesheet" href="/static/css/style.css">
<link rel="stylesheet" href="/static/css/a.css">
</head>

<body>

    <div class="html404">
        <div class="title">404</div>
            <div class="link">
                <div><a href="/">回首页</a>
                 </div>
            </div>
        </div>
   </div>
</body>

</html>

然后问题就解决了。

英文:

because i wrote 404 html liked this

&lt;link rel=&quot;stylesheet&quot; href=&quot;/static/css/style.css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;/static/css/a.css&quot;&gt;
&lt;div class=&quot;html404&quot;&gt;
    &lt;div class=&quot;title&quot;&gt;404&lt;/div&gt;
        &lt;div class=&quot;link&quot;&gt;
            &lt;div&gt;&lt;a href=&quot;/&quot;&gt;回首页&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

it caused the question. when i changed the html to

&lt;html&gt;

&lt;head&gt;
     &lt;title&gt;Aiysea 404 Page&lt;/title&gt;&lt;link rel=&quot;stylesheet&quot; href=&quot;/static/css/style.css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;/static/css/a.css&quot;&gt;
&lt;/head&gt;

&lt;body&gt;

    &lt;div class=&quot;html404&quot;&gt;
        &lt;div class=&quot;title&quot;&gt;404&lt;/div&gt;
            &lt;div class=&quot;link&quot;&gt;
                &lt;div&gt;&lt;a href=&quot;/&quot;&gt;回首页&lt;/a&gt;
                 &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
   &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

then it gone well.