我在使用Jinja2渲染模板时尝试使用utf-8字符.以下是我的模板的样子:
<!DOCTYPE HTML>
<html manifest="" lang="en-US">
<head>
<meta charset="UTF-8">
<title>{{title}}</title>
...
Run Code Online (Sandbox Code Playgroud)
标题变量设置如下:
index_variables = {'title':''}
index_variables['title'] = myvar.encode("utf8")
template = env.get_template('index.html')
index_file = open(preview_root + "/" + "index.html", "w")
index_file.write(
template.render(index_variables)
)
index_file.close()
Run Code Online (Sandbox Code Playgroud)
现在,问题是myvar是从消息队列中读取的消息,可以包含那些特殊的utf8字符(例如"SéptimoCine").
渲染的模板看起来像:
...
<title>S\u00e9ptimo Cine</title>
...
Run Code Online (Sandbox Code Playgroud)
我希望它是:
...
<title>Séptimo Cine</title>
...
Run Code Online (Sandbox Code Playgroud)
我已经做了几次测试,但我不能让它工作.
我试图设置title变量而没有.encode("utf8"),但它抛出异常(ValueError:期望一个字节对象,而不是unicode对象),所以我的猜测是初始消息是unicode
我使用chardet.detect来获取消息的编码(它是"ascii"),然后执行以下操作:myvar.decode("ascii").encode("cp852"),但标题仍未正确呈现.
我还确保我的模板是UTF-8文件,但它没有什么区别.
关于如何做到这一点的任何想法?
我正在研究一个也使用Redux的React Native应用程序,我想用Jest编写测试.我无法模仿反应导航添加的"导航"道具.
这是我的组件:
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { Text, View } from 'react-native';
const Loading = (props) => {
if (props.rehydrated === true) {
const { navigate } = props.navigation;
navigate('Main');
}
return (
<View>
<Text>Loading...</Text>
</View>
);
};
Loading.propTypes = {
rehydrated: PropTypes.bool.isRequired,
navigation: PropTypes.shape({
navigate: PropTypes.func.isRequired,
}).isRequired,
};
const mapStateToProps = state => ({
rehydrated: state.rehydrated,
});
export default connect(mapStateToProps)(Loading);
Run Code Online (Sandbox Code Playgroud)
Loading组件作为屏幕添加到DrawerNavigator.
以下是测试:
import React from 'react';
import …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 XMLRPC 连接到 Wordpress 博客。我正在使用最新的库 v2.3 ( http://python-wordpress-xmlrpc.readthedocs.org/en/latest/ )。
当我尝试初始化客户端时出现以下异常:
ServerConnectionError: <ProtocolError for www.myblogaddress.com/xmlrpc.php: 403 Forbidden>
Run Code Online (Sandbox Code Playgroud)
我注意到这种情况发生在检查用户名和密码之前,因此它与无效凭据没有任何关系。我相信它可能需要一些自定义标头,例如用户代理,但我不知道如何设置自定义传输参数。
我已经从 python-wordpress-xmlrpc 库复制了代码并对其进行了修改,以便我可以进行测试。这是我到目前为止所拥有的:
from xmlrpclib import Transport
class SpecialTransport(Transport):
def send_content(self, connection, request_body):
connection.putheader("Content-Type", "text/xml")
connection.putheader("Content-Length", str(len(request_body)))
connection.putheader('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11')
connection.putheader('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
connection.putheader('Accept-Charset','ISO-8859-1,utf-8;q=0.7,*;q=0.3')
connection.putheader('Accept-Encoding','none')
connection.putheader('Accept-Language', 'en-US,en;q=0.8')
connection.putheader('Connection', 'keep-alive')
connection.endheaders()
if request_body:
connection.send(request_body)
url = "{test_url_here}"
try:
server = xmlrpc_client.ServerProxy(url, allow_none=True, transport=SpecialTransport())
supported_methods = server.mt.supportedMethods()
except xmlrpc_client.ProtocolError as err:
print "A protocol error occurred"
print "URL: %s" …Run Code Online (Sandbox Code Playgroud) python ×2
python-2.7 ×2
jestjs ×1
jinja2 ×1
mocking ×1
react-native ×1
utf-8 ×1
wordpress ×1
xml-rpc ×1