# ui.status_code | NiceGUI

[Button: icon:menu]

[](/zh)

[安装](/zh#installation)

[特性](/zh#features)

[演示](/zh#demos)

[文档](/zh/documentation)

[示例](/zh/examples)

[为什么？](/zh#why)

[Button]

[Button]

[Button]

[](https://github.com/zauberzeug/nicegui/)

[Button: icon:more_vert]

# ui.*status_code*

## Status code

Set the HTTP status code for the current page response.
Must be called during page building, before the response is sent to the client.

*Added in version 3.10.0*

:code: HTTP status code (e.g. 200, 404, 503)

main.py

[Button]

````python
from nicegui import ui

@ui.page('/teapot')
def teapot_page():
    ui.status_code(418)
    ui.label("I'm a teapot")

@ui.page('/')
def page():
    ui.link('Visit the teapot page', '/teapot')

ui.run()
````

## Conditional 404

You can use `ui.status_code` to return a 404 status code when an item is not found.
This is useful for SEO and for signaling to clients that a page does not exist.

main.py

[Button]

````python
from nicegui import ui

@ui.page('/item/{item_id}')
def item_page(item_id: str):
    if 'a' <= item_id <= 'c':
        ui.label(f'This is item {item_id}')
    else:
        ui.status_code(404)
        ui.label('Page not found')

@ui.page('/')
def page():
    ui.link('Show item A', '/item/a')
    ui.link('Show item B', '/item/b')
    ui.link('Show item X', '/item/x')

ui.run()
````

**Nice**GUI

在浏览器中呈现的 Python UI 框架。

[](https://github.com/zauberzeug/nicegui/)

[](https://discord.gg/TEpFeAaF4f)

[](https://www.reddit.com/r/nicegui/)

资源

[文档](/zh/documentation)

[示例](/zh/examples)

[LLM 参考](/llms.txt)

[GitHub](https://github.com/zauberzeug/nicegui/)

[PyPI](https://pypi.org/project/nicegui/)

社区

[讨论区](https://github.com/zauberzeug/nicegui/discussions)

[Discord](https://discord.gg/TEpFeAaF4f)

[Reddit](https://www.reddit.com/r/nicegui/)

[参与贡献](https://github.com/zauberzeug/nicegui/blob/main/CONTRIBUTING.md)

[赞助者](https://github.com/sponsors/zauberzeug)

法律

[法律声明](/zh/imprint_privacy#imprint)

[隐私](/zh/imprint_privacy#privacy)

由 [Zauberzeug](https://zauberzeug.com) 用 NiceGUI 打造

© 2026 [Zauberzeug GmbH](https://zauberzeug.com)