python flask:自定义404子模板只呈现父模板的htmlfi

2024-04-19 16:00:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我的主页.html很好用。我做了我的子模板,404自定义错误页继承主页.html文件。但是,当我运行它时,错误页继承主页.html而不会在404.html上呈现其余的html。我做错什么了?在

站点.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template("home.html")

@app.route('/blog/')
def blog():
    return render_template("home.html")

@app.route('/about/')
def about():
    return render_template("about.html")

@app.route('/projects/')
def projects():
    return render_template("projects.html")

# not working 
@app.errorhandler(404)
def page_not_found(e):
    return render_template("404.html"), 404

if __name__ == '__main__':
    app.run(debug=True)

主页.html(家长):

^{pr2}$

404.html(子级):

{% extends "home.html" %}
{% block title %}Page Not Found{% endblock %}
{% block body %}
  <h1>404 Error: Page Not Found</h1>
  <p>What you were looking for is just not there.
  <p><a href="{{ url_for('index') }}">go somewhere nice</a>
{% endblock %} 

Tags: nameappflaskhomereturndefhtml错误
1条回答
网友
1楼 · 发布于 2024-04-19 16:00:00

认为您忘记在您的home.html中添加{% block %}标记吗

<!Doctype html>
<html>
<head>
{% block title %}
<title>Katherine Low's Blog</title>
{% endblock %}
<style>
.menu {
    text-align: center;
    font-size: 1em;
    margin: 10%;
}
.button {
  display: inline-block;
  width: 20%;  
  height: 20%;
}
#current_page {
    text-decoration: underline;
}
a {
    text-decoration: none;
    color: black;
}
a:hover {
    text-decoration: underline;
}
a:active {
    text-decoration: underline;
}
</style>
</head>
<body>
{% block body %}

<div class="menu">
    <div class="button" id="current_page"><a href="http://localhost:5000/blog/" />BLOG</div>
    <div class ="button"><a href="http://localhost:5000/about/">KATHERINE LOW</a></div>
    <div class="button"><a href="http://localhost:5000/projects/">PROJECTS</a></div>
</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
  window.jQuery || document.write('<script src="{{
  url_for('static', filename='jquery.js') }}">\x3C/script>')</script>
<script type="text/javascript"> 
  $(document).ready(function(){
    $('.button').hover(
      function(){
        $('#current_page').css("text-decoration", "none");
      }, function() {
        $('#current_page').css("text-decoration", "underline");
      }
    );

  });
{% endblock %}
</script>
</body>
</html>

相关问题 更多 >