小编cal*_*iph的帖子

Django Admin更改小部件类型

我想将我的many2many关系的widget类型更改为"FilteredSelectMultiple"

我从其他问题和文档中了解到这应该可以解决问题

admin.py
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.db import models
from django import forms

class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
            forms.ModelMultipleChoiceField: {'widget': FilteredSelectMultiple},
    }
Run Code Online (Sandbox Code Playgroud)

不幸的是,管理页面没有任何变化.有人可以帮忙吗?

django django-admin

3
推荐指数
1
解决办法
1636
查看次数

JQuery:延迟加载JQuery和'$ not defined'

我无法解决这个问题以及我在网上发现的大量信息:

在我的项目中,JQuery加载了"defer".由于项目标准,我无法改变.

<script defer src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js'></script>
Run Code Online (Sandbox Code Playgroud)

现在我需要在页面中添加一些小函数(当前是内联的):

使用此设置,浏览器将尝试在jQuery加载之前执行内联脚本=>"未捕获的ReferenceError:$未定义"

<body>
...
...
<script>
  $("#city").change(function() {...some stuff...};
  $("#doctor").change(function() {...some stuff...};
</script>
</body>
Run Code Online (Sandbox Code Playgroud)

什么是解决这个问题的聪明方法?

javascript jquery

3
推荐指数
1
解决办法
7600
查看次数

dialogflow python SDK:如何解析 DetectIntentResponse

我正在从 dialogflow V1 移动到 V2。使用 dialogflow python SDK,我收到一个 DetectIntentResponse 结构对象,其中应该包含我需要的信息。

在尝试查找文档并尝试检查此对象一段时间后,我需要您的帮助。这个对象远远超出我的范围......

对于文档,这就是我获取响应对象的方式:

import dialogflow_v2 as dialogflow
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
text_input = dialogflow.types.TextInput(text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(session=session, query_input=query_input)
Run Code Online (Sandbox Code Playgroud)

如何解析响应?

例如,我通过使用获得了一些参数结构response.query_result.parameters但是我如何获得这个列表?

也许我可以将响应转换为 json(这会让事情变得很容易)?

我需要字典、列表、字符串... :)

python dialogflow-es

3
推荐指数
1
解决办法
1521
查看次数

python3 记录器 - UnicodeEncodeError

我有一个这样的记录器设置:

import logging
from logging.handlers import RotatingFileHandler
import sys

# root logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# create a file logger
handler = RotatingFileHandler('log/core-application.log', maxBytes=1024*1024*1, backupCount=3)
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(funcName)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)


# create stdout logger
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(funcName)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

logger.info('Logging all setup')
Run Code Online (Sandbox Code Playgroud)

在我的开发系统上,这个日志条目工作正常。它记录您在此处看到的内容:

logger.info('message key: {}'.format('2018-10-19_00:20:56_\xd8\xa7\xd9\x84\xd8\xb5\xd9\x88\xd8\xa7\xd9\x81/'))

我的 ubuntu AWS EC2 服务器上的同一行给了我一个错误:

---记录错误---回溯(最近通话最后一个):文件“/usr/lib/python3.5/logging/初始化的.py”,线路982,在EMIT stream.write(MSG)UnicodeEncodeError: 'ASCII'编解码器无法对位置 108-113 中的字符进行编码:序号不在范围内 (128)

任何人都可以想象这是为什么?

顺便说一句:EC2 服务器区域设置是: …

python python-3.x

3
推荐指数
2
解决办法
3927
查看次数

python fabric:put()导致权限被拒绝

我使用织物并具有:

put('/projects/configuration-management/prototype','/etc/nginx/sites-available')
Run Code Online (Sandbox Code Playgroud)

结果是:

Underlying exception:
    Permission denied

Aborting.
Run Code Online (Sandbox Code Playgroud)

其他配置文件可以轻松上传。我怎样才能避免我的问题?

python fabric

2
推荐指数
1
解决办法
1807
查看次数

如何从dialogflow API访问protobuf响应中的信息

我在访问访问 googleDialogflow api 后收到的 protobuf 对象的值时遇到严重问题

(... create google cloud session object ...)

text_input = dialogflow.types.TextInput(text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(request={"session": session, "query_input": query_input})

from google.protobuf import json_format
response_json = json_format.MessageToDict(response)
Run Code Online (Sandbox Code Playgroud)

错误是:

AttributeError: 'MapComposite' object has no attribute 'DESCRIPTOR'

基本上我有两个问题:

a)我无法将 protobuf 转换为 json (在这里我可以轻松地看到如何访问我正在寻找的信息)

b) 我无法理解 protobof 数据结构。有字典吗?

a) 或 b) 中的任何帮助者均表示赞赏。

顺便说一句:我正在寻找一种response.parameters在 API 响应中访问的方法。


斯科特的编辑:

response.parameters 是一个对象: <proto.marshal.collections.maps.MapComposite object at 0x7fe7323f7da0>

参数对象如下所述: https: //cloud.google.com/dialogflow/es/docs/reference/rest/v2/DetectIntentResponse#QueryResult

这对你来说有意义吗?因为我不明白如何访问这个对象中的值。

python protocol-buffers dialogflow-es

2
推荐指数
1
解决办法
2987
查看次数

flask-admin:美化 json 字段

我真的很喜欢 flask-admin 的 CRUD 功能。不幸的是,我还没有找到使 json DB 字段更具可读性的方法。

JSON 字段在列表视图(以及编辑视图)中显示为文本字符串。

任何人都可以给我任何指示从哪里开始美化列表视图(以及后来的编辑视图)?

flask flask-admin

1
推荐指数
1
解决办法
1156
查看次数

python beautifulsoup:用字符串中的url替换链接

在包含 HTML 的字符串中,我有几个想要用纯 href 值替换的链接:

from bs4 import BeautifulSoup
a = "<a href='www.google.com'>foo</a> some text <a href='www.bing.com'>bar</a> some <br> text'
soup = BeautifulSoup(html, "html.parser")

tags = soup.find_all()
for tag in tags:
  if tag.has_attr('href'):
    html = html.replace(str(tag), tag['href'])
Run Code Online (Sandbox Code Playgroud)

不幸的是,这会产生一些问题:

  • html 中的标签使用单引号',但 beautifulsoup 将使用带引号 ( )str(tag)的标签创建。所以不会找到匹配的。"<a href="www.google.com">foo</a>replace()
  • <br>被识别为<br/>. 再次replace()找不到匹配项。

所以看来使用python的replace()方法不会给出可靠的结果。

有没有办法使用 beautifulsoup 的方法用字符串替换标签?

编辑:

str(tag) 的附加值 =<a href="www.google.com">foo</a>

python beautifulsoup

1
推荐指数
1
解决办法
2647
查看次数