我在Flask和SQLAlchemy中有一个非常简单的模型,公司在玩火柴.匹配由主机和来宾定义.我不知道如何将主机和访客公司带到模板,我正在获取他们的ID.
代码是这样的:
class Company(db.Model):
__tablename__ = 'companies'
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(64), unique = True)
address = db.Column(db.String(120), unique = False)
website = db.Column(db.String(100), unique = False)
...
class Match(db.Model):
__tablename__ = 'matches'
id = db.Column(db.Integer, primary_key = True)
local_id =db.Column(db.Integer, db.ForeignKey('companies.id'))
guest_id = db.Column(db.Integer, db.ForeignKey('companies.id'))
match_time = db.Column(db.DateTime()) # not important
Run Code Online (Sandbox Code Playgroud)
我希望能够在模板中执行以下操作:
{{ match.host.name }} - {{ match.guest.name }}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
我使用PyQt4.QtWebkit来抓取我的django应用程序中的网页.在生产环境中该模块无法抓取它.它会抛出错误"manage.py:无法连接到X服务器"
我的Qt班:
class Render(QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.mainFrame().load(QUrl(url))
self.app.exec_()
def _loadFinished(self, result):
self.frame = self.mainFrame()
self.app.quit()
Run Code Online (Sandbox Code Playgroud)
从django-shell调用:
r = Render(url)
Run Code Online (Sandbox Code Playgroud)
当我使用Django-shell(python manage.py shell)通过django调用这个"Render"类时,render函数会抛出错误.你能帮帮我吗?
这是我的OrderedDict字典:
>>> dic = OrderedDict([('16-17,1,5,BUY,BUY,-6,9', 10), ('19-20,1,1,BUY,BUY,-1,1',1)])
>>> dic
OrderedDict([('16-17,1,5,BUY,BUY,-6,9', 10), ('19-20,1,1,BUY,BUY,-1,1', 11)])
>>> for k,v in dic.iteritems():
... print k
... k1 = k.split(',')
... print k1
... print "value",v
... print k1.append(v)
...
16-17,1,5,BUY,BUY,-6,9
['16-17', '1', '5', 'BUY', 'BUY', '-6', '9']
value 10
None
19-20,1,1,BUY,BUY,-1,1
['19-20', '1', '1', 'BUY', 'BUY', '-1', '1']
value 11
None
Run Code Online (Sandbox Code Playgroud)
为什么打印"无"?
>>> from PyQt4.QtCore import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PyQt4.QtCore
Run Code Online (Sandbox Code Playgroud)
我一直在尝试安装PyQt4.QtWebKit,但遇到了问题.我遵循了这些指示.
我可以做安装(./ configure,make,make install)SIP.我只是不能在PyQt中做到这一点.
错误是:
ubuntu@ip-10-32-157-231:~/Desktop/build/PyQt$ sudo python configure.py --verbose -q /usr/bin/qmake
Determining the layout of your Qt installation...
/usr/bin/qmake -o qtdirs.mk qtdirs.pro
make -f qtdirs.mk
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_CORE_LIB -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4 -I. -o qtdirs.o qtdirs.cpp
qtdirs.cpp:1:28: fatal error: QCoreApplication: No such file or directory
compilation terminated.
make: *** [qtdirs.o] …Run Code Online (Sandbox Code Playgroud) 我需要将给定文本中的正则表达式模式与 python 进行匹配。
正文是:
"""
2010 Toyota FJ Cruiser FJ CRUISER
Int. Color:
Ext. Color:
Black
Trans:
Automatic
VIN:
JTEZU4BF7AK009445
Stock:
122821B
DIFFERENTIALBLACK
Status:
Body Style:
SUV
Engine:
Gas V6 4.0L/241
Dealership: Universal Toyota
$29,988*
Price
View More Information
Compare?
"""
Run Code Online (Sandbox Code Playgroud)
从这段文本中我需要提取“JTEZU4BF7AK009445”(长度为17)这个模式在vin之后:可能
我用了这个模式
vin_pattern = re.compile('([A-Z0-9]{17})')
vin = re.findall(vin_pattern,text)
["JTEZU4BF7AK009445","DIFFERENTIALBLACK"]
Run Code Online (Sandbox Code Playgroud)
但DIFFERENTIALBLACK不应该匹配
我也使用了该模式
price_pat = re.compile('(\$[0-9\,\.]+)')
Run Code Online (Sandbox Code Playgroud)
匹配价格范围(“$”符号+值)
这里我只需要在VIN_PATTERN出现50个字符之前和之后检查这个价格匹配模式。因为在某些情况下我有更多的价格值。所以,我需要过滤 VIN 模式存在的 50 个字符之前和 50 个字符之后的文本
请问应该怎么做?
我在Windows环境中使用Python 2.6来完成我目前的工作.但我对Python 2.7中添加的一些功能感兴趣.是否可以将某些功能(特别是"Collections.OrderedDict")从2.7移动到2.6?如果是这样我们怎么能这样做?
我需要执行一个shell脚本来运行via python命令中的python程序.
我应该像这样执行我的python脚本
ubuntu@ip-10-32-157-231:~/hg_intcen/lib$ xvfb-run python webpage_scrapper.py http://www.google.ca/search?q=navaspot
Run Code Online (Sandbox Code Playgroud)
这个脚本需要在python程序中执行,因为必须将巨大的链接传递给该模块.
我已经搜索过在python中执行这个shell脚本,所以我使用了"subprocess"
主要的是当你运行这个shell命令时,它需要一些时间来返回结果.我需要python模块来执行此命令,并且它必须等待while返回结果.这是必需的.
我使用subprocess.Popen它不会像我从bash那样返回结果
import subprocess
def execute_scrapping(url):
exe_cmd = "xvfb-run python lib/webpage_scrapper.py"+" "+str(url)
print "cmd:"+exe_cmd
proc = subprocess.Popen(exe_cmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
time.sleep(15)
sys.stdout.flush()
d=proc.stdout.readlines()
return d[1]
Run Code Online (Sandbox Code Playgroud)
以上内容未达到确切结果.你能建议我通过python执行bash shell命令并得到结果吗?
我在list_display中添加了一个小函数来显示多个到多个字段,我试图显示由换行符分隔的m2m对象.但换行分离器没有工作
我的django管理员:
class ArticleAdmin(admin.ModelAdmin):
list_filter = ["category",]
list_display = ('category','article_type', 'get_domains')
filter_horizontal = ("domain",)
def get_domains(self, obj):
x ="\n".join([str(s.name) for s in obj.domain.all()])
print x # printing fine
return x
get_domains.short_description = 'Domains'
Run Code Online (Sandbox Code Playgroud)
print语句做得很好:
0 errors found
Django version 1.4.5, using settings 'settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
mykitchenremodelinglosangeles.com:8000
kitchenremodelerchicagoil.com:8000
localhost:8000
Run Code Online (Sandbox Code Playgroud)
我已经附加了管理屏幕输出:

你能建议一下吗?
我有字符串.
st = "12345 hai how r u @3456? Awer12345 7890"
re.findall('([0-9]+)',st)
Run Code Online (Sandbox Code Playgroud)
它应该不会像:
['12345', '3456', '12345', '7890']
Run Code Online (Sandbox Code Playgroud)
我应该得到
['12345','7890']
Run Code Online (Sandbox Code Playgroud)
我应该只取数值
和
它不应该包含任何其他字符,如字母,特殊字符
在我的python项目中,登录过程由HttpBasicAuth处理程序处理.得到响应后,我应该通过selenium加载主页面.
它应该如何完成?
它有baseurl.当基本URL由python-selenium加载时,弹出框会询问用于身份验证的用户名和密码.然后它进入主页面.
以下是两个步骤:
我使用HttpBasicAuth处理程序的原因是,当我打开我的URL时,它会打开一个用于登录的弹出窗口.因为,我们不知道由Selenium处理的弹出模式对话窗口的控制,我们切换到HttpAuthentication.
(这是我需要建议的部分.)
在这个中间部分,我使用来自HttpBasicAuth处理程序的响应进行身份验证,以便Selenium可以继续测试中的其他步骤.
如何在Selenium中对站点进行身份验证?我是否必须使用HttpBasicAuthentication Response为Selenium webdriver创建一个cookie,以便跳过登录框?
我想将字符串拆分为整数和运算符,以便在python中进行Infix表达式求值.
这是我的字符串:
>>> s = (1-2+3)*5+10/2
Run Code Online (Sandbox Code Playgroud)
我试过这个分裂:
>>>list(s)
['(', '1', '-', '2', '+', '3', ')', '*', '5', '+', '1', '0', '/', '2']
Run Code Online (Sandbox Code Playgroud)
这是错的.因为'10'被分成'1','0'
我试过替代方案:
>>> re.findall('[+-/*//()]+|\d+',s)
['(', '1', '-', '2', '+', '3', ')*', '5', '+', '10', '/', '2']
Run Code Online (Sandbox Code Playgroud)
这也是错的.因为')*'应该分成')','*'
你能帮忙从给定的表达式中拆分运算符和整数吗?
list1 = ['Contact: Hamdan Z Hamdan, MBBS, Msc',
'\r\n ',
'+249912468264',
'\r\n ',
'hamdanology@hotmail.com',
'\r\n ',
'Contact: Maha I Mohammed, MBBS, PhD',
'\r\n ',
'+249912230895',
'\r\n ',
'\r\n ',
'Sudan',
'Jaber abo aliz',
'\r\n ',
'Recruiting',
'\r\n ',
'Khartoum, Sudan, 1111 ',
u'Contact: Khaled H Bakheet, MD,PhD \xa0 \xa0 +249912957764 \xa0 \xa0 ',
'khalid2_3456@yahoo.com',
u' \xa0 \xa0 ',
u'Principal Investigator: Hamdan Z Hamdan, MBBS,MSc \xa0 \xa0 \xa0 \xa0 \xa0 \xa0 ',
'Principal Investigator:',
'\r\n ',
'Hamdan Z Hamdan, MBBS, MSc', …Run Code Online (Sandbox Code Playgroud) python ×11
regex ×4
django ×2
django-admin ×2
pyqt4 ×2
bash ×1
flask ×1
installation ×1
pyqt ×1
python-2.6 ×1
python-2.7 ×1
selenium ×1
shell ×1
sqlalchemy ×1
string ×1
subprocess ×1
ubuntu ×1
ubuntu-11.04 ×1
windows ×1