网页
综合
更新时间 2026-11-14 09:30:28
Quickstart — Flask Documentation (3.1.x)
自动蜘蛛
flask.palletsprojects.com
正文约 5259 字
Eager to get started? This page gives a good introduction to Flask. Follow Installation to set up a project and install Flask first.
A minimal Flask application looks something like this:
So what did that code do?
First we imported the Flask class. An instance of this class will be our WSGI application.
Next we create an instance of this class. The first argument is the name of the application’s module or package. __name__ is a convenient shortcut for this that is appropriate for most cases. This is needed so that Flask knows where to look for resources such as templates and static files.
We then use the route() decorator to tell Flask what URL should trigger our function.
The function returns the message we want to display in the user’s browser. The default content type is HTML, so HTML in the string will be rendered by the browser.
Save it as hello.py or something similar. Make sure to not call your application flask.py because this would conflict with Flask itself.
To run the application, use the flask command or python -m flask. You need to tell the Flask where your application is with the --app option.
Application Discovery Behavior
As a shortcut, if the file is named app.py or wsgi.py, you don’t have to use --app. See Command Line Interface for more details.
This launches a very simple builtin server, which is good enough for testing but probably not what you want to use in production. For deployment options see Deploying to Production.
Now head over to http://127.0.0.1:5000/, and you should see your hello world greeting.
If another program is already using port 5000, you’ll see OSError: [Errno 98] or OSError: [WinError 10013] when the server tries to start. See Address already in use for how to handle that.
Externally Visible Server
If you run the server you will notice that the server is only accessible from your own computer, not from any other in the network. This is the default because in debugging mode a user of the application can execute arbitrary Python code on your computer.
If you have the debugger disabled or trust the users on your network, you can make the server publicly available simply by adding --host=0.0.0.0 to the command line:
This tells your operating system to listen on all public IPs.
The flask run command can do more than just start the development server. By enabling debug mode, the server will automatically reload if code changes, and will show an interactive debugger in the browser if an error occurs during a request.
The debugger allows executing arbitrary Python code from the browser. It is protected by a pin, but still represents a major security risk. Do not run the development server or debugger in a production environment.
To enable debug mode, use the --debug option.
Development Server and Command Line Interface for information about running in debug mode.
Debugging Application Errors for information about using the built-in debugger and other debuggers.
Logging and Handling Application Errors to log errors and display nice error pages.
When returning HTML (the default response type in Flask), any user-provided values rendered in the output must be escaped to protect from injection attacks. HTML templates rendered with Jinja, introduced later, will do this automatically.
escape(), shown here, can be used manually. It is omitted in most examples for brevity, but you should always be aware of how you’re using untrusted data.
If a user submits /hello?name=<script>alert("bad")</script>, escaping causes it to be rendered as text, rather than running the script in the user’s browser.
Modern web applications use meaningful URLs to help users. Users are more likely to like a page and come back if the page uses a meaningful URL they can remember and use to directly visit a page.
Use the route() decorator to bind a function to a URL.
You can do more! You can make parts of the URL dynamic and attach multiple rules to a function.
You can add variable sections to a URL by marking sections with <variable_name>. Your function then receives the <variable_name> as a keyword argument. Optionally, you can use a converter to specify the type of the argument like <converter:variable_name>.
(default) accepts any text without a slash
accepts positive integers
accepts positive floating point values
like string but also accepts slashes
accepts UUID strings
The following two rules differ in their use of a trailing slash.
The canonical URL for the projects endpoint has a trailing slash. It’s similar to a folder in a file system. If you access the URL without a trailing slash (/projects), Flask redirects you to the canonical URL with the trailing slash (/projects/).
The canonical URL for the about endpoint does not have a trailing slash. It’s similar to the pathname of a file. Accessing the URL with a trailing slash (/about/) produces a 404 “Not Found” error. This helps keep URLs unique for these resources, which helps search engines avoid indexing the same page twice.
To build a URL to a specific function, use the url_for() function. It accepts the name of the function as its first argument and any number of keyword arguments, each corresponding to a variable part of the URL rule. Unknown variable parts are appended to the URL as query parameters.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。