1
Python Flask中PluggableViews和Blueprint有什麼區別?Python Flask中可插入視圖和藍圖的區別
Python Flask中PluggableViews和Blueprint有什麼區別?Python Flask中可插入視圖和藍圖的區別
我不知道比較它們正確與否,但
根據瓶Documentation:
瓶0.7引入了由Django的是基於類的,而不是通用的觀點啓發可插拔意見的功能。主要目的是可以替換部分實現,並且這種方式具有可定製的可插入視圖。
在該示例中,它定義視圖中的get_template_name方法和再使用它在其他視圖。這就是可插入視圖的用途。
from flask.views import View
class ListView(View):
def get_template_name(self):
raise NotImplementedError()
def render_template(self, context):
return render_template(self.get_template_name(), **context)
def dispatch_request(self):
context = {'objects': self.get_objects()}
return self.render_template(context)
class UserView(ListView):
def get_template_name(self):
return 'users.html'
def get_objects(self):
return User.query.all()
燒瓶藍圖只是組織大型項目的一種更簡單的方法。他們不會爲您提供Pluggable Views功能。
from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound
simple_page = Blueprint('simple_page', __name__,
template_folder='templates')
@simple_page.route('/', defaults={'page': 'index'})
@simple_page.route('/<page>')
def show(page):
try:
return render_template('pages/%s.html' % page)
except TemplateNotFound:
abort(404)
然後,您將這些藍圖註冊到您的應用程序對象。
AFAIK,藍圖是路線。您可以將0到多個可插入視圖加載到單個模板中。 –
如果您熟悉Django,Flask可插入視圖就像基於類的Django泛型視圖,您可以編寫可重用的視圖,但藍圖只是更簡單的方式來組織更大的Flask項目,並且不會爲您提供此功能。 http://flask.pocoo.org/docs/0.11/views/ –