demo code

from flask import Flask, url_for, request
from flask import render_template

app = Flask(__name__)

@app.route('/')
def index():
    return "index page"

@app.route('/user/<username>')
def show_user_profile(username):
    return "user: %s"%username

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        return "do_the_login()"
    else:
        return "show_the_login_form()"

@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
    return render_template('hello.html', name=name)

if __name__ == '__main__':
    with app.test_request_context():
        print url_for('static', filename='style.css')
     #   print( url_for('index') )
      #  print( url_for('login') )
       # print( url_for('login', id=100) )
       # print( url_for('profile', username='JohnDoe') )

    app.run(host='10.3.13.251',debug=True)
#    app.run(host='10.3.13.251')

hello.html

<!doctype html>
<title>Hello from Flask</title>
{% if name %}
  <h1>Hello {{ name }}!</h1>
{% else %}
  <h1>Hello World!</h1>
{% endif %}

base.html

<html>
<head>
    {% block head %}
    <title>{% block title %}{% endblock %} my application </title>
    {% endblock %}}
</head>
<body>
    {% block body %}
    {% endblock %}
</body>
</html>


你可能感兴趣的:(demo code)