根据Django文档,可以在when子句中使用多个条件。
When(
registered_on__gt=date(2014, 1, 1),
registered_on__lt=date(2015, 1, 1),
then='account_type'
)
Run Code Online (Sandbox Code Playgroud)
但是,在使用Case子句时,我无法使用相同的内容。
Case(
When(
registered_on__gt=date(2014, 1, 1),
registered_on__lt=date(2015, 1, 1),
then='account_type'
),
default='default'
)
Run Code Online (Sandbox Code Playgroud)
我最终收到以下错误:
TypeError: __init__() got multiple values for keyword argument 'then'
有什么办法可以做到这一点?我在这里想念什么吗?
在 Python 中记录异常的最正确方法是什么?
有些人只记录e
另一个日志属性e.message
(但在某些例外情况下可能会丢失)。
一种用于str()
捕获异常的描述,但它不包含错误类型,如果没有错误类型,有时就没用。
实际上repr()
会返回错误类型和描述,但它并不那么流行(在我看到的项目中)。
还有一个相关问题:这如何影响像 Sentry 这样收集和分组错误/警报的服务。它们还应该包含一些有关具体错误的信息。
因此想再次开启这个讨论:哪种记录异常的方式最好使用?
def foo():
""" Method that can raise various types of exceptions """
1/0 # just for example
try:
foo()
except Exception as e:
logger.exception('Error occurred during execution: %s', e) # 1
logger.exception('Error occurred during execution: %s', e.message) # 2
logger.exception('Error occurred during execution: %s', str(e)) # 3
logger.exception('Error occurred during execution: %s', str(e.message)) # 4
logger.exception('Error occurred during execution: %s', repr(e)) # …
Run Code Online (Sandbox Code Playgroud) 我有一个基本视图,用于检索一些数据、呈现我的页面并将一些数据发送到此页面:
def myview(request)
one = values.objects.get(user=request.user).address
two = values.objects.get(user=request.user).number
return render(request, "main/mytemplate.html",
context={'address': one, 'numbers': two})
Run Code Online (Sandbox Code Playgroud)
因此,这两个查询检索到的值显示在我的页面上。
现在,在名为mytemplate.html的同一页面上,我正在使用另一个视图,该视图应该处理表单和其他一些操作:
def secondview(request):
if request.method == 'POST':
if 'button1' in request.POST:
form = MyForm(request.POST)
# check whether it's valid:
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
return HttpResponseRedirect(request.path_info)
else:
form = MyForm()
return HttpResponse('it works!')
Run Code Online (Sandbox Code Playgroud)
如何在第二个视图中使用这两个查询检索到的数据?当第一个视图加载页面时执行查询。然后,在同一页面中使用第二个视图。我想用两个变量one
,并two
在第二视图。有没有办法在 Django 中做到这一点?
为什么不在第二个视图中进行相同的查询?因为我希望第二种形式在重新加载方面尽可能快,而不必每次使用该视图时都进行数据库查询。此外,由于我已经在打开页面时检索了这些值,因此再次这样做会很浪费。
我不知道这个问题是否足够清楚,但它的核心是:我可以在 django 中的两个视图之间传递变量/数据吗?
我有一个基于Django的网站。我想将其中包含模式的URL重定向servertest
到相同的URL,但servertest
应替换为server-test
。
因此,例如,以下URL将被映射为重定向,如下所示:
http://acme.com/servertest/ => http://acme.com/server-test/
http://acme.com/servertest/www.example.com => http://acme.com/server-test/www.example.com
http://acme.com/servertest/www.example.com:8833 => http://acme.com/server-test/www.example.com:8833
Run Code Online (Sandbox Code Playgroud)
我可以使用urls.py中的以下行来获取第一个示例:
http://acme.com/servertest/ => http://acme.com/server-test/
http://acme.com/servertest/www.example.com => http://acme.com/server-test/www.example.com
http://acme.com/servertest/www.example.com:8833 => http://acme.com/server-test/www.example.com:8833
Run Code Online (Sandbox Code Playgroud)
不确定如何为其他用户执行此操作,因此仅替换了URL的servetest部分。
我的Python代码给出了这个错误:
from cv_bridge import CvBridge, CvBridgeError
导入错误:libcv_bridge.so:无法打开共享对象文件:没有这样的文件或目录。
但是,如果我做 ldd,一切似乎都很好。怎么了?(ROS indigo、ubuntu 14.04 LTS、PyCharm)
user@user-VirtualBox:/opt/ros/indigo/lib$ ldd libcv_bridge.so
linux-vdso.so.1 => (0x00007ffc9439d000)
libopencv_imgproc.so.2.4 => /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4 (0x00007fcac6b34000)
libopencv_highgui.so.2.4 => /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4 (0x00007fcac68e9000)
libopencv_core.so.2.4 => /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4 (0x00007fcac64b1000)
libopencv_contrib.so.2.4 => /usr/lib/x86_64-linux-gnu/libopencv_contrib.so.2.4 (0x00007fcac61ce000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fcac5eca000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fcac5cb3000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcac58ee000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fcac56d0000)
libtbb.so.2 => /usr/lib/libtbb.so.2 (0x00007fcac549b000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fcac5195000)
libGL.so.1 => /var/lib/VBoxGuestAdditions/lib/libGL.so.1 (0x00007fcac4f5d000)
libjpeg.so.8 => /usr/lib/x86_64-linux-gnu/libjpeg.so.8 (0x00007fcac4d07000)
libpng12.so.0 => /lib/x86_64-linux-gnu/libpng12.so.0 (0x00007fcac4ae1000)
libtiff.so.5 => /usr/lib/x86_64-linux-gnu/libtiff.so.5 (0x00007fcac486f000)
libjasper.so.1 => /usr/lib/x86_64-linux-gnu/libjasper.so.1 (0x00007fcac4617000)
libIlmImf.so.6 => …
Run Code Online (Sandbox Code Playgroud) 如果我有用户聊天 ID,如何检查用户是否在线?我尝试过bot.userStatus(myChat);
,但这是错误的。
如何删除我的机器人发送给用户的消息?如果消息包含按钮会影响它们吗?它们会消失吗?删除按钮的最佳方法是什么?
我正在使用 python 3.7 和电子邮件,imap 库读取电子邮件并提取电子邮件和附件的内容,所有附件(如 excel、csv、pdf)都作为附件下载,但是当我在电子邮件中收到任何 .eml 文件时,它显示错误,请找到以下代码以读取电子邮件内容和附件,如果收到 eml 文件作为附件,则会显示错误。它在写入 eml 文件时显示错误。在写入时 part.get_payload(decode=True) 在 eml 文件中变为空白。
filename = part.get_filename()
if filename is not None:
dot_position = filename.find('.')
file_prefix = filename[0:dot_position]
file_suffix = filename[dot_position:len(filename)]
# print(dot_position)
# print(file_prefix)
# print(file_suffix)
now = datetime.datetime.now()
timestamp = str(now.strftime("%Y%m%d%H%M%S%f"))
newFileName = file_prefix + "_" + timestamp + file_suffix
sv_path = os.path.join(svdir, newFileName)
# allfiles = allfiles.append([{"oldfilename": filename, "newfilename": newFileName}])
mydict = filename + '$$' + newFileName
mydict1 = mydict1 + ',' + mydict
print(mydict1) …
Run Code Online (Sandbox Code Playgroud) 我想制作一个按钮,可以从 Telegram 聊天中在浏览器中打开 URL(外部超链接)。目前,我只开发了可点击的操作按钮。
update.message.reply_text(
'Subscribe to us on Facebook and Telegram:',
reply_markup=InlineKeyboardMarkup([
[InlineKeyboardButton(text='on Facebook', callback_data='Facebook')],
[InlineKeyboardButton(text='on Telegram', callback_data='Telegram')],
])
)
Run Code Online (Sandbox Code Playgroud)
但如何将它们作为链接(带箭头)。我想请求用户分享。
我正在使用 PHP 通过 Viber REST API 创建 Viber 机器人。我试图了解如何创建 Viber 的键盘设计集,但无济于事。如何让它看起来像 Viber 提供的键盘设计示例一样?
[
[
'Columns' => 3,
'Rows' => 2,
'Text' => '<font color=\"#494E67\">Smoking</font><br><br>',
'TextSize' => 'medium',
'TextHAlign' => 'center',
'TextVAlign' => 'bottom',
'ActionType' => 'reply',
'ActionBody' => 'Smoking',
'BgColor' => '#f7bb3f',
'Image' => 'https://img.icons8.com/material-outlined/24/000000/smoking.png',
],
[
'Columns' => 3,
'Rows' => 2,
'Text' => '<font color=\"#494E67\">Non Smoking</font><br><br>',
'TextSize' => 'medium',
'TextHAlign' => 'center',
'TextVAlign' => 'bottom',
'ActionType' => 'reply',
'ActionBody' => 'Non smoking',
'BgColor' => '#f6f7f9',
'Image' => …
Run Code Online (Sandbox Code Playgroud) python ×7
django ×3
telegram-bot ×2
viber ×2
viber-api ×2
viber-bot ×2
django-forms ×1
django-orm ×1
django-urls ×1
django-views ×1
imaplib ×1
ldd ×1
python-3.7 ×1
python-3.x ×1
ros ×1
telegram ×1