Templates in Flask

Source: Internet
Author: User
Tags exception handling
Templates

Under the Templates folder

Syntax is {{}} Get Parameters

<! DOCTYPE html>
1 2 3 4 5 6 7 8 9 10 11
__author__ = ' Nicholas '

class User ():
    def __init__ (self,name,password):
        self.name = name
        Self.password = password

    def getName (self): return
        self.name

    def setname (self,name):
        self.name = name

    def SetPassword (self,password):
        self.password = password

__author__ = ' Nicholas ' from
flask Import flask,render_template from
model import User
app = Flask (__name__)


@app. Route ('/')
def Hello (): Context
    = "Hello template ' return
    render_template" (' index.html ', context = context)


@app. Route ('/user ')
def user ():
    user = User (' Zhangsan ', ' Zhangsan ') return
    render_template (' index.html ', user = user)

if __ name__== ' __main__ ':
    app.run ()
1, 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 Conditional Statement

Syntax format

{% if condition%}

{% Else%}

{% ENDIF%}
1 2 3 4 5 6
{% if user%}
    Hello, {{user.getname ()}}
{% Else%}
    sorry,no such user!
{% ENDIF%}
1 2 3 4 5 Loop Statement

Syntax format

{% for user in users%}
{% ENDFOR%}
1 2 3
{% for user in users%}
    {{user.getname ()}}---{{User.password}}
{% endfor%}
1 2 3 Template Inheritance

The invariant part of a page is abstracted into a base class, and the changed part is implemented in subclasses

Base class base.html

<! DOCTYPE html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Sub Class

One.html

<!--inherits the parent class-->
{% extends "base.html"%}

<!--implements a part of the parent class-->
{% block context%}
    
1 2 3 4 5 6 7 8 message hints and exception handling message flash mechanismDeclaration App.secret_key = ' 123 ' calls flush
From flask import flask, Flash, render_template, request,abort

app = Flask (__name__)
App.secret_key = ' 123 '


@app. Route ('/')
def hello_world ():
    Flash ("Hello") return
    render_template ("index.html")

@ App.route ('/login ', methods=[' POST ')
def login ():
    form = request.form
    username = form.get (' username ')
    password = form.get (' password ')

    if not username:
        Flash ("Please input username") return
        Render_ Template ("index.html")
    if not password:
        Flash (' Please input password ') return
        render_template (" Index.html ")

    if username = = ' Weixuan ' and password = = ' 123456 ':
        Flash (" Login Success ") return
        Render_ Template ("index.html")
    else:
        Flash ("username or password is wrong") return
        render_template (" Index.html ")

if __name__ = = ' __main__ ':
    app.run ()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<! DOCTYPE html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 Exception Handling

@app. ErrorHandler (404) Handling Exceptions

@app. ErrorHandler (404)
def not_found (e): Return
    render_template ("404.html")

@app. Route ('/users/ <user_id> ')

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.