Python weppy全栈的 Python Web 框架

联合创作 · 2023-09-20

weppy 是一个全栈的 Python Web 框架,weppy 非常灵巧,易于理解学习和使用。兼容 Python 2.7, 3.3, 3.4, 3.5 和 3.6.

示例代码:

from weppy import App, request, response
from weppy.orm import Database, Model, Field
from weppy.tools import service, requires

class Task(Model):
    name = Field('string')
    is_completed = Field('bool', default=False)

app = App(__name__)
app.config.db.uri = "postgres://user:password@localhost/foo"
db = Database(app)
db.define_models(Task)
app.pipeline = [db.pipe]

def is_authenticated():
    return request.headers["Api-Key"] == "foobar"

def not_authorized():
    response.status = 401
    return {'error': 'not authorized'}

@app.route(methods='get')
@service.json
@requires(is_authenticated, otherwise=not_authorized)
def todo():
    page = request.query_params.page or 1
    tasks = Task.where(
        lambda t: t.is_completed == False
    ).select(paginate=(page, 20))
    return {'tasks': tasks}

weppy 可以让你直接用 Python 编写模板:

{{extend 'layout.html'}}

<div class="post-list">
{{for post in posts:}}
    <div class="post">
        <h2>{{=post.title}}</h2>
    </div>
{{pass}}
{{if not posts:}}
    <div>
        <em>No posts here so far.</em>
    </div>
{{pass}}
</div>













浏览 3
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报