目前,我使用以下方法在Pyramid中上传文件(通过HTML表单).
if request.params.get('form.submitted'):
upload_directory = os.getcwd() + '/myapp/static/uploads/'
my_file = request.POST.get('thumbnail')
saved_file = str(upload_directory) + str(my_file.filename)
perm_file = open(saved_file, 'w')
shutil.copyfileobj(my_file.file, perm_file)
my_file.file.close()
perm_file.close()
Run Code Online (Sandbox Code Playgroud)
我只是想知道,这是保存文件上传的好方法,我的方法有任何安全问题吗?我怎么能改进我的方法.谢谢.
我希望在一个Pyramid实例上有多个域和子域.但是,我似乎无法找到任何文件.最后一个问题涉及一个资料很少,没有例子的词汇表.你们有没有任何例子或者可以指导我更好的文档?
我有一个问题,我得到一个错误,如这个:
"MyPyramidApplication Error"<class 'sqlalchemy.orm.exc.StaleDataError'>: DELETE statement on table 'page_view' expected to delete 6 row(s); Only 0 were matched.
Run Code Online (Sandbox Code Playgroud)
所以,我很清楚是什么导致了这个问题,但我一直无法解决它.
我有一个page_view模型,它有一个外键page_id和a user_id.
这是模型的样子:
page_view_table = sa.Table(
'page_view',
metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('page_id', sa.Integer, sa.ForeignKey('guide.id')),
sa.Column('user_id', sa.Integer, sa.ForeignKey('user.id')),
sa.Column('last_view', sa.DateTime, nullable=False),
sa.UniqueConstraint('user_id', 'page_id'),
mysql_engine='InnoDB',
mysql_charset='utf8mb4'
)
Run Code Online (Sandbox Code Playgroud)
这是关系的样子
orm.mapper(Page, page_table,
properties = {
'users_viewed': sa.orm.relation(
User,
secondary=page_view_table,
backref='page'),
}
)
Run Code Online (Sandbox Code Playgroud)
我使用insert语句将一些项添加到我的数据库,类似于:
ins = model.page_view_table.insert()
sql = str(ins)
sql += ' ON DUPLICATE KEY UPDATE last_view = :last_view'
session = model.Session() …Run Code Online (Sandbox Code Playgroud) 在金字塔结构,功能route_path和route_url用于生成从路由结构的网址.所以,如果我有路线:
config.add_route('idea', 'ideas/{idea}')
Run Code Online (Sandbox Code Playgroud)
我可以使用它生成url
request.route_url('idea', idea="great");
Run Code Online (Sandbox Code Playgroud)
但是,有时我可能想添加额外的get参数来生成url,如:
idea/great?sort=asc
Run Code Online (Sandbox Code Playgroud)
这该怎么做?
我试过了
request.route_url('idea', idea='great', sort='asc')
Run Code Online (Sandbox Code Playgroud)
但那没用.
我想保留development.ini并production.ini受版本控制,但出于安全原因,不希望sqlalchemy.url存储连接字符串,因为这将包含用于数据库连接的用户名和密码.
在金字塔中,从一个额外的外部文件中获取此设置的规范方法是什么?
编辑 除了使用环境变量的解决方案之外,我在询问#pyramid之后想出了这个解决方案:
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
# Read db password from config file outside of version control
secret_cfg = ConfigParser()
secret_cfg.read(settings['secrets'])
dbpass = secret_cfg.get("secrets", "dbpass")
settings['sqlalchemy.url'] = settings['connstr'] % (dbpass,)
Run Code Online (Sandbox Code Playgroud) 金字塔中模板上下文的等价物是什么?
金字塔中的IBeforeRender活动是否与此有关?我已经阅读了官方文档,但很难理解IBeforeRender事件究竟是什么.
我有以下jquery代码:
$.ajax({
type: 'POST',
url: url,
data: data,
dataType: 'json',
statusCode: {
200: function (data, textStatus, jqXHR) {
console.log(data);
},
201: function (data, textStatus, jqXHR) {
log(data);
},
400: function(data, textStatus, jqXHR) {
log(data);
},
},
});
Run Code Online (Sandbox Code Playgroud)
当后端(金字塔)中的验证失败时使用400.现在从Pyramid我如何返回HTTPBadRequest()响应以及包含验证错误的json数据?我尝试过类似的东西:
response = HTTPBadRequest(body=str(error_dict)))
response.content_type = 'application/json'
return response
Run Code Online (Sandbox Code Playgroud)
但是当我在firebug中检查时,它返回400(错误请求),这是好的,但它永远不会从上面的data.responseText解析json响应.
我想为我的项目做国际化.我按照官方文档中描述的方式进行了描述,但本地化仍无效.以下是我尝试获取用户区域设置的方法:
def get_locale_name(request):
""" Return the :term:`locale name` associated with the current
request (possibly cached)."""
locale_name = getattr(request, 'locale_name', None)
if locale_name is None:
locale_name = negotiate_locale_name(request)
request.locale_name = locale_name
return locale_name
Run Code Online (Sandbox Code Playgroud)
但是request没有attr"local_name",但它有"Accept-Language",因此当函数get_local_name在请求中找不到"local_name"时,它会调用另一个函数:
def negotiate_locale_name(request):
""" Negotiate and return the :term:`locale name` associated with
the current request (never cached)."""
try:
registry = request.registry
except AttributeError:
registry = get_current_registry()
negotiator = registry.queryUtility(ILocaleNegotiator,
default=default_locale_negotiator)
locale_name = negotiator(request)
if locale_name is None:
settings = registry.settings or {}
locale_name = …Run Code Online (Sandbox Code Playgroud) 是否可以运行Pyramid pserve,以便启动https服务器(例如https://0.0.0.0:6543)?
如果可能的话,我想在本地设置https应用程序.
我正在创建一个基于Pyramid框架的移动服务.因为它是移动的,所以减少带宽使用的一切都是加分.我正在考虑gzipping所有流量,甚至动态HTML页面.
Pyramid框架为此提供了什么样的钩子?或者是否有WSGI中间件用于任务?我想在Python级别上做这个,而不是Nginx/Apache,所以我可以更好地统计gzip带来多少好处.
pyramid ×10
python ×9
pylons ×2
sqlalchemy ×2
database ×1
gzip ×1
http ×1
https ×1
jquery ×1
localization ×1
multipart ×1
mysql ×1
server ×1
url-routing ×1