【问题标题】:How to make an error page for non existent articles如何为不存在的文章制作错误页面
【发布时间】:2021-01-16 00:23:42
【问题描述】:

如果该程序中不存在文章,如何制作错误页面?此错误代码仅在 404 错误时返回它,因为我希望它在用户尝试访问的文章不存在时返回此模板。

所以基本上如果用户搜索“/wiki/x”。 x 是用户输入的任何内容,如果不存在我想返回此错误模板。

我找不到很多关于如何在 python 中使用瓶子处理错误页面的信息,因此我们将不胜感激。

@route('/wiki/<pagename>')
def show_article(pagename):
    """Displays a single article (loaded from a text file)."""
    articles=read_articles_from_file()
    for article in articles:
        if article['title'] == pagename:
            titel = article
    return template('wiki', article = titel)
@error(404)
def error404(error):
    '''
    Funktionen ger ett felmeddelande, ifall de kommer
    till en sida som inte existerar
    '''
    return template("error")

【问题讨论】:

    标签: python bottle


    【解决方案1】:

    你应该在完成你的循环后测试你是否找到了一篇文章,如果没有像这样呈现你的错误页面:

    @route('/wiki/<pagename>')
    def show_article(pagename):
        """Displays a single article (loaded from a text file)."""
        articles=read_articles_from_file()
        titel = None
        for article in articles:
            if article['title'] == pagename:
                titel = article
        if titel == None:
            return template("error")
        return template('wiki', article = titel)
    

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 2011-04-20
      • 2018-11-15
      相关资源
      最近更新 更多