批处理脚本:关闭空间和用户输入的某些字符(Batch Script: Closes on space and certain characters from user input)
在我创建的批处理脚本中,某些时候它会查看用户输入并根据IF语句执行相应的功能。 如果用户输入不匹配,则会再次请求输入。 我正在运行的问题是,如果用户输入空格,<,>,;等,程序突然终止。 我已经尝试添加if语句来捕获它们并添加@ECHO ON以查看交互但它似乎就像用户按下回车一样死掉。 有关如何避免这种情况的任何想法?
In the batch script I've created there are certain times where it looks at user input and does functions accordingly based on IF statements. If the user input doesn't match it will ask for input again. The issue I'm running across is if the user inputs a space, <, >, ;, etc. the program abruptly terminates. I've tried adding if statements to catch them and added @ECHO ON to look at the interaction but it seems to die just as the user presses enter. Any ideas on how to avoid this?
原文:https://stackoverflow.com/questions/18242537
最满意答案
我的表单太具体了(它应该是客户端而不是client_id),所以我的工作代码如下所示:
会议:
from zope.sqlalchemy import ZopeTransactionExtension DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension())) Base = declarative_base()
模型:
class Client(Base): __tablename__ = 'client' id = Column(Integer, primary_key=True, nullable=False) name = Column(Unicode(48)) street = Column(Unicode(48)) city = Column(Unicode(32)) task = relationship("Task", backref="client") @classmethod def active(cls): return DBSession.query(Client).options(load_only("id", "name")).order_by(sa.desc(Client.name)).filter(Client.status == True) class Task(Base): __tablename__ = 'task' id = Column(Integer, primary_key=True, nullable=False) name = Column(String(48)) status = Column(Boolean) client_id = Column(Integer, ForeignKey('client.id'))
形成:
def enabled_client(): return Client.active() class TaskCreateForm(ModelForm): name = TextField('Task name', [validators.Length(min=1, max=48)], filters=[strip_filter]) status = BooleanField('Status') client = QuerySelectField('Client', query_factory=enabled_client, get_label='name', allow_blank=False)
视图:
@view_config(route_name='task_action', match_param='action=create', renderer='arx:templates/task_edit.mako', permission='edit') def task_create(request): task = Task() form = TaskCreateForm(request.POST) if request.method == 'POST' and form.validate(): form.populate_obj(task) DBSession.add(task) return HTTPFound(location=request.route_url('home')) return {'form':form, 'action':request.matchdict.get('action')}
My form was too specific (it should be client instead of client_id), so my working code looks like this:
Session:
from zope.sqlalchemy import ZopeTransactionExtension DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension())) Base = declarative_base()
Model:
class Client(Base): __tablename__ = 'client' id = Column(Integer, primary_key=True, nullable=False) name = Column(Unicode(48)) street = Column(Unicode(48)) city = Column(Unicode(32)) task = relationship("Task", backref="client") @classmethod def active(cls): return DBSession.query(Client).options(load_only("id", "name")).order_by(sa.desc(Client.name)).filter(Client.status == True) class Task(Base): __tablename__ = 'task' id = Column(Integer, primary_key=True, nullable=False) name = Column(String(48)) status = Column(Boolean) client_id = Column(Integer, ForeignKey('client.id'))
Form:
def enabled_client(): return Client.active() class TaskCreateForm(ModelForm): name = TextField('Task name', [validators.Length(min=1, max=48)], filters=[strip_filter]) status = BooleanField('Status') client = QuerySelectField('Client', query_factory=enabled_client, get_label='name', allow_blank=False)
View:
@view_config(route_name='task_action', match_param='action=create', renderer='arx:templates/task_edit.mako', permission='edit') def task_create(request): task = Task() form = TaskCreateForm(request.POST) if request.method == 'POST' and form.validate(): form.populate_obj(task) DBSession.add(task) return HTTPFound(location=request.route_url('home')) return {'form':form, 'action':request.matchdict.get('action')}
相关问答
更多-
在这种情况下,您可以使用WTForms中的扩展。 你要做的是导入你需要的QuerySelectField : from wtforms.ext.sqlalchemy.fields import QuerySelectField 然后你创建一个函数来查询数据库并返回你需要的东西: def skill_level_choices(): return db.session.query(SkillLevel).all() 获得查询对象后,可以使用query_factory参数将其放入query ...
-
我的表单太具体了(它应该是客户端而不是client_id),所以我的工作代码如下所示: 会议: from zope.sqlalchemy import ZopeTransactionExtension DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension())) Base = declarative_base() 模型: class Client(Base): __tablename__ = 'cl ...
-
从wtforms文档: class wtforms.fields.SelectField(default field arguments, choices=[], coerce=unicode, option_widget=None) """ Select fields keep a choices property which is a sequence of (value, label) pairs. The value portion can be any type in ...
-
具有wtforms_sqlalchemy.orm.model_form的日期对象(Date objects with wtforms_sqlalchemy.orm.model_form)[2024-01-27]
我认为问题不在于Model或SqlAlchemy,而是使用表单。 正如我可以看到你的日期列是DateTime(不是日期)。 下面是Form可以解释问题的一个小例子: # flask forms uses the same fields from wtforms import Form, DateTimeField class ArticleForm(Form): date = DateTimeField('Date') form_data = MultiDict() # your format ... -
报告表? 你的意思是下拉列表吗? 你试过了吗 project_list = QuerySelectField('Project', query_factory=get_projects, get_label='project_id') 检查此问题可将查询减少为仅选择所需的列。 Finally I find what was wrong with my code :) I was using project_id(Foreign Key) in the report table to obtain the ...
-
相当于Go的WTForms?(WTForms equivalent for Go?)[2022-06-04]
没有像WTForms for Go(还)那样“完整”:你会发现Go软件包比Flask的模块更加模块化。 您可以使用goforms或大猩猩/模式将POST表单覆盖到Go结构。 我还建议sqlx作为数据库/ sql之上的一个很好的填充程序。 There is not something as "complete" as WTForms for Go (yet): you will find that Go packages are a little more modular than even Flask's. ... -
对该字段使用的查询应用排序。 给定一个模型如 class Car(Base): ... make = Column(Unicode, nullable=False) model = Column(Unicode, nullable=False) 您会传递一个查询或查询工厂来生成有序结果: def cars(): return session.query(Car).order_by(Car.make, Car.model) class Cars(Form): car ...
-
如何填充与WTForms和SQLAlchemy的多对多关系?(How do I populate many-to-many relationship with WTForms and SQLAlchemy?)[2023-08-02]
摆脱'()'。 它需要一个方法工厂 members = QuerySelectMultipleField(query_factory=get_members) get rid of the '()'. it requires a method factory members = QuerySelectMultipleField(query_factory=get_members) -
对此的答案是我分叉代码并自己进行更改。 https://github.com/havok2063/wtforms-alchemy The answer to this was that I forked the code and made the changes myself. https://github.com/havok2063/wtforms-alchemy
-
您需要将default关键字参数设置为要作为默认值的Authors实例: # Hypothetically, let's say that the current user makes the most sense # This is just an example, for the sake of the thing user = Authors.get(current_user.id) ContentForm.author = QuerySelectField('Author', get_label=' ...