Site backend _python+flask.0009.flask The default and custom resource directory for static resources?

Source: Internet
Author: User

Default location:

Description: Most Web applications will provide a CDN-based static file service to give users a better experience, static files mainly include css/js/Pictures/fonts, etc., flask by default only need to create "static" directory under the project root directory, the project uses based on/ The path to the static start can be accessed, but for better processing power, it is recommended to use Nginx or another Web server to manage static files


Recommended Solutions:

1. Do not write the static file path directly in the project, you should use Url_for to generate the path, of course, if we want to specify the path can also specify the Static_folder parameter when creating the app instance

#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "# Description: Import public module from Flask import flask, url_for, jsonify# Description: Import other modules app = Flask (__name__, static_folder= ' static ') @app. Rout E ('/') def index (): Return jsonify (url_for (' Static ', filename= ' img/girl.jpg ', _external=true)) if __name__ = = ' __main__ ' : App.run (host= ' 0.0.0.0 ', port=9000, Debug=true)

Note: The static_folder specified when creating an app instance above is relative to the root directory (Flask.helpers.get_root_path), supports any specified static file directory, Url_for () The first parameter static when generating the URL is actually the default view function in App.url_map, so it can be passed directly, filename is the parameter that the static view function must have, and the relative URL path is returned by default, if a full URL path is required under some special scenarios ( such as registration confirmation email) can return full URL path by specifying _external=true



This article is from "@Can up and No BB ..." Blog, be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1866524

Site backend _python+flask.0009.flask The default and custom resource directory for static resources?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.