- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在学习 Pyramid 教程:http://pyramid-blogr.readthedocs.io/en/latest/project_structure.html并使用 Windows 8。
当前结果:当我打开本地主机地址(在 firefox 中)时,出现以下错误:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: entries [SQL: 'SELECT entries.id AS entries_id, entries.title AS entries_title, entries.body AS entries_body, entries.created AS entries_created, entries.edited AS entries_edited \nFROM entries ORDER BY entries.created DESC\n LIMIT ? OFFSET ?'] [parameters: (5, 0)]
追溯(最近的调用最后):
File "C:\Python34\Lib\site-packages\pyramid_debugtoolbar\panels\performance.py", line 71, in noresource_timer_handler
result = handler(request)
File "C:\Python34\Lib\site-packages\pyramid\tweens.py", line 62, in excview_tween
reraise(*attrs['exc_info'])
File "C:\Python34\Lib\site-packages\pyramid\compat.py", line 148, in reraise
raise value
File "C:\Python34\Lib\site-packages\pyramid\tweens.py", line 22, in excview_tween
response = handler(request)
File "C:\Python34\Lib\site-packages\pyramid_tm\__init__.py", line 119, in tm_tween
reraise(*exc_info)
File "C:\Python34\Lib\site-packages\pyramid_tm\compat.py", line 15, in reraise
raise value
File "C:\Python34\Lib\site-packages\pyramid_tm\__init__.py", line 98, in tm_tween
response = handler(request)
File "C:\Python34\Lib\site-packages\pyramid\router.py", line 158, in handle_request
view_name
File "C:\Python34\Lib\site-packages\pyramid\view.py", line 547, in _call_view
response = view_callable(context, request)
File "C:\Python34\Lib\site-packages\pyramid\viewderivers.py", line 442, in rendered_view
result = view(context, request)
File "C:\Python34\Lib\site-packages\pyramid\viewderivers.py", line 147, in _requestonly_view
response = view(request)
File "c:\projects\turing_chat\turing_blog\turing_blog\views\default.py", line 9, in index_page
paginator = BlogRecordService.get_paginator(request, page)
File "c:\projects\turing_chat\turing_blog\turing_blog\services\blog_record.py", line 30, in get_paginator
url_maker=url_maker)
File "C:\Python34\Lib\site-packages\paginate_sqlalchemy\__init__.py", line 35, in __init__
super(SqlalchemyOrmPage, self).__init__(*args, wrapper_class=SqlalchemyOrmWrapper, **kwargs)
File "C:\Python34\Lib\site-packages\paginate\__init__.py", line 251, in __init__
self.items = list(self.collection[first:last])
File "C:\Python34\Lib\site-packages\paginate_sqlalchemy\__init__.py", line 20, in __getitem__
return self.obj[range]
File "C:\Python34\Lib\site-packages\sqlalchemy\orm\query.py", line 2489, in __getitem__
return list(res)
File "C:\Python34\Lib\site-packages\sqlalchemy\orm\query.py", line 2797, in __iter__
return self._execute_and_instances(context)
File "C:\Python34\Lib\site-packages\sqlalchemy\orm\query.py", line 2820, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "C:\Python34\Lib\site-packages\sqlalchemy\engine\base.py", line 945, in execute
return meth(self, multiparams, params)
File "C:\Python34\Lib\site-packages\sqlalchemy\sql\elements.py", line 263, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Python34\Lib\site-packages\sqlalchemy\engine\base.py", line 1053, in _execute_clauseelement
compiled_sql, distilled_params
File "C:\Python34\Lib\site-packages\sqlalchemy\engine\base.py", line 1189, in _execute_context
context)
File "C:\Python34\Lib\site-packages\sqlalchemy\engine\base.py", line 1393, in _handle_dbapi_exception
exc_info
File "C:\Python34\Lib\site-packages\sqlalchemy\util\compat.py", line 202, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Python34\Lib\site-packages\sqlalchemy\util\compat.py", line 185, in reraise
raise value.with_traceback(tb)
File "C:\Python34\Lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context
context)
File "C:\Python34\Lib\site-packages\sqlalchemy\engine\default.py", line 469, in do_execute
cursor.execute(statement, parameters) sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: entries [SQL: 'SELECT entries.id AS entries_id, entries.title AS entries_title, entries.body AS entries_body, entries.created AS entries_created, entries.edited AS entries_edited \nFROM entries ORDER BY entries.created DESC\n LIMIT ? OFFSET ?'] [parameters: (5, 0)]
预期结果:本教程指出,当我打开我的本地主机地址时,我应该看到一个包含文本的页面:“未找到博客条目”。
这些是我在教程中创建/编辑的一些文件的文件内容:
blog_record.py
> import sqlalchemy as sa from paginate_sqlalchemy import
> SqlalchemyOrmPage #<- provides pagination from ..models.blog_record
> import BlogRecord
>
>
> class BlogRecordService(object):
>
> @classmethod
> def all(cls, request):
> query = request.dbsession.query(BlogRecord)
> return query.order_by(sa.desc(BlogRecord.created)) # ^^ will return a query object that can return an entire dataset(in desc order)
> when needed.
> @classmethod
> def by_id(cls, _id, request):
> query = request.dbsession.query(BlogRecord)
> return query.get(_id) # ^^ return a single entry by id or the None object if nothing is found
> @classmethod
> def get_paginator(cls, request, page=1):
> query = request.dbsession.query(BlogRecord)
> query = query.order_by(sa.desc(BlogRecord.created))
> query_params = request.GET.mixed()
>
> def url_maker(link_page):
> # replace page param with values generated by paginator
> query_params['page'] = link_page
> return request.current_route_url(_query=query_params)
>
> return SqlalchemyOrmPage(query, page, items_per_page=5,
> url_maker=url_maker)
默认.py
from pyramid.view import view_config
from ..services.blog_record import BlogRecordService
@view_config(route_name='home',
renderer='pyramid_blogr:templates/index.jinja2')
def index_page(request):
page = int(request.params.get('page', 1))
paginator = BlogRecordService.get_paginator(request, page)
return {'paginator': paginator}
# ^^ We first retrieve from the URL's request object the page number that we want to present to the user.
# If the page number is not present, it defaults to 1.
# The paginator object returned by BlogRecord.get_paginator will then be used in the template
# to build a nice list of entries.
# Everything we return from our views in dictionaries will be available in templates as variables.
# So if we return {'foo':1, 'bar':2}, then we will be able to access the variables
# inside the template directly as foo and bar.
@view_config(route_name='auth', match_param='action=in', renderer='string',
request_method='POST')
@view_config(route_name='auth', match_param='action=out', renderer='string')
def sign_in_out(request):
return {}
用户.py
import datetime #<- will be used to set default dates on models
from turing_blog.models.meta import Base #<- we need to import our sqlalchemy metadata from which model classes will inherit
from sqlalchemy import (
Column,
Integer,
Unicode, #<- will provide Unicode field
UnicodeText, #<- will provide Unicode text field
DateTime, #<- time abstraction field
)
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(Unicode(255), unique=True, nullable=False)
password = Column(Unicode(255), nullable=False)
last_logged = Column(DateTime, default=datetime.datetime.utcnow)
模型/__init__.py
from sqlalchemy import engine_from_config
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import configure_mappers
import zope.sqlalchemy
# import or define all models here to ensure they are attached to the
# Base.metadata prior to any initialization routines
from .user import User
from .blog_record import BlogRecord
# run configure_mappers after defining all of the models to ensure
# all relationships can be setup
configure_mappers()
def get_engine(settings, prefix='sqlalchemy.'):
return engine_from_config(settings, prefix)
def get_session_factory(engine):
factory = sessionmaker()
factory.configure(bind=engine)
return factory
def get_tm_session(session_factory, transaction_manager):
"""
Get a ``sqlalchemy.orm.Session`` instance backed by a transaction.
This function will hook the session to the transaction manager which
will take care of committing any changes.
- When using pyramid_tm it will automatically be committed or aborted
depending on whether an exception is raised.
- When using scripts you should wrap the session in a manager yourself.
For example::
import transaction
engine = get_engine(settings)
session_factory = get_session_factory(engine)
with transaction.manager:
dbsession = get_tm_session(session_factory, transaction.manager)
"""
dbsession = session_factory()
zope.sqlalchemy.register(
dbsession, transaction_manager=transaction_manager)
return dbsession
def includeme(config):
"""
Initialize the model for a Pyramid app.
Activate this setup using ``config.include('turing_blog.models')``.
"""
settings = config.get_settings()
# use pyramid_tm to hook the transaction lifecycle to the request
config.include('pyramid_tm')
session_factory = get_session_factory(get_engine(settings))
config.registry['dbsession_factory'] = session_factory
# make request.dbsession available for use in Pyramid
config.add_request_method(
# r.tm is the transaction manager used by pyramid_tm
lambda r: get_tm_session(session_factory, r.tm),
'dbsession',
reify=True
)
models/blog_record.py
import datetime #<- will be used to set default dates on models
from turing_blog.models.meta import Base #<- we need to import our sqlalchemy metadata from which model classes will inherit
from sqlalchemy import (
Column,
Integer,
Unicode, #<- will provide Unicode field
UnicodeText, #<- will provide Unicode text field
DateTime, #<- time abstraction field
)
from webhelpers2.text import urlify #<- will generate slugs e.g. converts "foo Bar Baz" to "foo-Bar-Baz"
from webhelpers2.date import distance_of_time_in_words #<- human friendly dates
class BlogRecord(Base):
__tablename__ = 'entries'
id = Column(Integer, primary_key=True)
title = Column(Unicode(255), unique=True, nullable=False)
body = Column(UnicodeText, default=u'')
created = Column(DateTime, default=datetime.datetime.utcnow)
edited = Column(DateTime, default=datetime.datetime.utcnow)
@property
def slug(self):
return urlify(self.title)
@property
def created_in_words(self):
return distance_of_time_in_words(self.created,
datetime.datetime.utcnow())
我怀疑我犯了一个简单的错误导致了这个错误,例如没有正确安装 Pyramid。如果有人可以帮助我,那就太好了。我也可以提供更多详细信息。
最佳答案
您需要运行创建数据库表的 initialize_pyramid_blogr_db
脚本。
你应该在某个地方有 virtualenv 文件夹,并且有 bin
或 Scripts
文件夹,Python 包安装它们的命令行脚本。找到 initialize_pyramid_blogr_db
和它的完整文件系统路径。
1) 确保您已激活虚拟环境 (activate.bat
)
2) 然后在你的项目文件夹中运行::
python \path\bin\initialize_pyramid_blogr_db developmnt.ini
关于python - Pyramid :sqlalchemy.exc.OperationalError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41905148/
Pyramid 项目中有一个 development.ini 或 production.ini 。我将自己的配置数据添加到 ini 文件如: [thrift] host = 0.0.0.0 port
我想注册一个请求处理程序,但不想使用扫描方法。 为什么我需要调用两个方法(add_route 和 add_view)而不是一个? from wsgiref.simple_server import m
请问我错过了什么。当我想提供已下载到磁盘上的视频文件时,我不断在浏览器中收到内部服务器错误。这是我的代码: View 函数 @view_config(name='download_video') de
请问我错过了什么。当我想提供已下载到磁盘上的视频文件时,我不断在浏览器中收到内部服务器错误。这是我的代码: View 函数 @view_config(name='download_video') de
我目前正在学习如何使用 Python Pyramid Web 框架,并且发现文档非常出色。 然而,在区分“模型”(即在 SQLAlchemy 声明性系统下定义的类)的概念和“资源”(即定义访问控制的方
我一直在尝试让 Pyramid 在谷歌应用程序引擎中运行,但没有成功。我尝试按照说明 here 进行操作但它似乎已经过时了,因为 gae 不再有 appcfg.py 了。我按照应用程序引擎文档中的 F
大多数可用教程都展示了如何使用上游 HTTP 服务器(如 NGINX)设置 uWSGI。但是 uWSGI 本身就可以完美地充当路由器/代理/负载均衡器 - 请参阅 this对于我的项目,我现在不想设置
我正在尝试使用 Pyramid 自省(introspection)接口(interface)从可调用 View 中获取给定资源类型的所有 View 的列表。我可以使用以下方法获取一组已添加的 View
我正在使用 Pyramid 来创建网络应用程序。然后我使用 Pyramid 烧杯将烧杯连接到 Pyramid 的 session 管理系统。 有两个值会影响用户 session 的持续时间。 sess
背景 我对 unicode 和 Python 真是一团糟。这似乎是一个普遍的焦虑,我尝试过使用其他解决方案,但我就是无法解决这个问题。 设置 MySQL 数据库设置 collation_datab
模型 - View - PHP 框架(如 Kohana)的 Controller 的 Pyramid/Python 等价物是什么? In Pyramid "Model" is .... and it
我遵循了 http://docs.pylonsproject.org/docs/pyramid/en/latest/tutorials/wiki/index.html 上的教程 我知道,当我添加或更改
我使用 yapps 为 Pyramid 内的 LaTex 语言生成解析器(例如将 \begin{itemize} 之类的内容翻译成相应的 -Tags)。一个命令(即 \ref{SOMEID} )应该
我正在 Pyramid 框架之上使用 python 制作 webapps。 在我利用 Mechanize 进行一些简单网页抓取的函数之一中,当我将其作为独立的 Python 脚本运行并通过 Pyram
var z = []; for(var i = 1; i len) z.push("a".repeat(len-i%len)) console.log(z.join("\n")); 关于jav
我正在开发 Python Pyramid我需要使用rest api,在其中一个请求中,我需要处理一个excel,对于它的每一行,我都会获取GPS坐标并进行大量验证,这意味着这个唯一的请求可能需要大约1
这是我的 base.html: {% block head %} {% endblock %} {% block body
我一直在尝试使用 Pyramid 框架制作带有复选框和单选按钮的表单,但我不知道如何正确执行。 我正在使用pyramid_simpleform。到目前为止,我已经能够使用 for 循环将复选框放在表单
我按照 Pyramid 教程进行操作,一切正常。然后我为 Pyramid 安装了 jinja2,并将必要的代码行添加到我的 development.ini 文件中。按预期在指定位置找到了我的模板。它们
我的应用程序从用户接收一个或多个 URL(通常为 3-4 个 URL),从这些 URL 中抓取某些数据并将这些数据写入数据库。但是,因为抓取这些数据需要一点时间,所以我正在考虑在单独的线程中运行每个抓
我是一名优秀的程序员,十分优秀!