# -*- coding: utf-8 -*- from __future__ import absolute_import from app import app app.run()
# -*- coding: utf-8 -*- from __future__ import absolute_import import os from datetime import timedelta _basedir = os.path.abspath(os.path.dirname(__file__)) DEBUG = True SECRET_KEY = os.urandom(24) PERMANENT_SESSION_LIFETIME=timedelta(seconds=24*60*60) CSRF_ENABLED = True CSRF_SESSION_KEY = SECRET_KEY
# -*- coding: utf-8 -*- from __future__ import absolute_import from flask import Flask, g, render_template, send_from_directory import os import os.path _basedir = os.path.abspath(os.path.dirname(__file__)) configPy=os.path.join(os.path.join( _basedir,os.path.pardir), 'config.py') app = Flask(__name__) # create our application object app.config.from_pyfile(configPy) #app.debug=True #change some attribute after load configuration flask_sqlalchemy_used=True # 如果使用Flask-SQLAlchemy了 db = SQLAlchemy(app) #create a db (SQLAlchemy) object from our app object login_manager = LoginManager(app) #create a LoginManager Object from our app object #add our view as the login view to finish configuring the LoginManager login_manager.login_view = "users.login_view" #register the users module blueprint from app.users.views import mod as usersModule app.register_blueprint(usersModule) #register the tickets module blueprint from app.tickets.views import mod as ticketsModule app.register_blueprint(ticketsModule) def connect_db(): # 如果没使用Flask-SQLAlchemy if not flask_sqlalchemy_used: return sqlite3.connect('/path/to/database.db') else: return None @app.before_request def before_request(): """Make sure we are connected to the database each request.""" if not flask_sqlalchemy_used: g.db = connect_db() @app.teardown_request def teardown_request(response): """Closes the database again at the end of the request.""" if not flask_sqlalchemy_used: g.db.close() return response #***************** # controllers #***************** @app.route('/favicon.ico') def favicon(): return send_from_directory(os.path.join(app.root_path, 'static'), 'ico/favicon.ico') @app.errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 @app.route("/") def index(): return render_template('index.html')
# -*- coding: utf-8 -*- from __future__ import absolute_import from flask import Blueprint, render_template, flash, redirect, session, url_for, request, g from flask.ext.login import login_user, logout_user, login_required from app import app, db, login_manager from forms import LoginForm, RegistrationForm from app.users.models import User mod = Blueprint('users', __name__) #register the users blueprint module @login_manager.user_loader def load_user(user_id): return User.query.get(user_id) @mod.route('/login/', methods=('GET', 'POST')) def login_view(): form = LoginForm(request.form) if form.validate_on_submit(): user = form.get_user() login_user(user) flash("Logged in successfully.") return redirect(request.args.get("next") or url_for("index")) return render_template('users/login.html', form=form) @mod.route('/register/', methods=('GET', 'POST')) def register_view(): form = RegistrationForm(request.form) if form.validate_on_submit(): user = User() form.populate_obj(user) db.session.add(user) #使用SqlAlchemy保存 db.session.commit() login_user(user) return redirect(url_for('index')) return render_template('users/register.html', form=form) @login_required @mod.route('/logout/') def logout_view(): logout_user() return redirect(url_for('index'))
macros.html, 是个jinja2的宏文件, 我们在该文件中可定义一些宏, 供form页面调用, 用来渲染form的元素. 用法是, 在我们的html文件中, 引入这个宏文件即可.
{% macro form_field(field) -%} {% set with_label = kwargs.pop('with_label', False) %} {% set placeholder = '' %} {% if not with_label %} {% set placeholder = field.label.text %} {% endif %} <div class="control-group {% if field.errors %}error{% endif %}"> {% if with_label %} <label for="{{ field.id }}" class="control-label"> {{ field.label.text }}{% if field.flags.required %} *{% endif %}: </label> {% endif %} <div class="controls"> {% set class_ = kwargs.pop('class_', '') %} {% if field.flags.required %} {% set class_ = class_ + ' required' %} {% endif %} {% if field.type == 'BooleanField' %} <label class="checkbox"> {{ field(class_=class_, **kwargs) }} {{ field.label.text|safe }} </label> {% else %} {% if field.type in ('TextField', 'TextAreaField', 'PasswordField') %} {% set class_ = class_ + ' input-xlarge' %} {% elif field.type == 'FileField' %} {% set class_ = class_ + ' input-file' %} {% endif %} {% if field.type == 'SelectField' %} {{ field(class_=class_, **kwargs) }} {% else %} {{ field(class_=class_, placeholder=placeholder, **kwargs) }} {% endif %} {% endif %} {% if field.errors %} <span class="error help-inline">{{ field.errors|join(', ') }}</span> {% endif %} {% if field.description %} <p class="help-block">{{ field.description|safe }}</p> {% endif %} </div> </div> {%- endmacro %}