Flaskwebdevelopment-flask template 4-url_for () generate links in templates

Source: Internet
Author: User

This is used url_for() to simplify the operation of generating links in templates. Take the example of generating a static file link.

Url_for ()

Directly in the template to write the URL, when the page is complex and cumbersome, and may be dependent on the code, the code changes after the link is invalid.

URLs can be generated dynamically through the flask provided url_for() . This function returns the corresponding URL as a parameter with the name of the view function or the end name of the registered view function.

For example, in the current code:

url_for(‘index‘)Return \ ,

url_for(‘index‘, _external=True)Return http://localhost:5000/ .

One is the relative link, the app internal jump is enough, the other is the absolute link.

url_for()Also supports arguments passed into the view function, which url_for(‘user‘, name=‘john‘, _external=True) is returned http://localhost:5000/user/john . If the parameter passed in is not a parameter of the view function, then this parameter is placed as a query string after the link url_for(‘index‘, page=2) is returned /?page=2 .

Generate static file links with Url_for ()

Web apps not only involve Python and templates, but also some static files, like pictures, JavaScript files, CSS, and so on.

A reference to a static file in flask is treated as a special route: the /static/<filename> url_for(‘static‘, filename=‘css/styles.css‘, _external=True) URL that gets the call is: http://localhost:5000/static/css/styles.css .

By default, flask static looks for static files in the folder under the app root directory. In this folder can organize the static files of the storage path, you can add folders inside.

Here's how to add an icon to the underlying template favicon.ico :

{% block head%} {{super ()}}<link rel= "shortcut icon" href= "{{url_for (' static ', filename = ' Favicon.ico ')}}" Type= "Image/x-icon" ><link rel= "icon" href= "{{url_for (' static ', filename = ' Favicon.ico ')}}" Type= "Image/x-icon" >{% endblock%}

templates/base.htmladd this paragraph to the end of the unload head block and use it super() to preserve the contents of the parent template.

Note:

    • Code to 3d tag.
    • Next is the Flask-moment plug-in section, the plug-in does the operation of the main is to format the time, including automatic client-side cross-zone adjustment, while providing some calculation of time including the past, such as the function, In fact, moment.js in the flask under the package, temporarily feel no meaning, decided to skip. This is a pit 3.

Flaskwebdevelopment-flask template 4-url_for () generate links in templates

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.