我正在使用赛普拉斯来测试我的Web应用程序.
此代码段目前有效并将提交新内容:
describe('The Create Page', () => {
it('successfully creates a thing', () => {
cy.visit('/create')
cy.get('input[name=description]').type('Hello World')
cy.get('button#submit')
.click()
// After POST'ing this data via AJAX, the web app then
// receives the id of the new thing that was just created
// and redirects the user to /newthing/:id
// How do I test that this redirection worked?
})
})
Run Code Online (Sandbox Code Playgroud)
正如评论所示,我不知道如何测试重定向到新路由是否有效.我可以在浏览器模拟中看到重定向工作,我只是想为它添加一个测试.
谢谢!!!
感谢下面的答案,我有一个before_request功能,/login如果用户尚未登录,则会将用户重定向到:
这是我的before_request的副本:
@app.before_request
def before_request():
if 'logged_in' not in session and request.endpoint != 'login':
return redirect(url_for('login'))
Run Code Online (Sandbox Code Playgroud)
除非用户已登录,否则不会提供静态目录中的文件.
在我的/login页面上,我从/static目录中获取一个css文件,但由于这个原因无法加载before_request.
我使用apache mod_wsgi部署了这个应用程序,在我的apache配置文件中,我甚至将该/static目录作为站点的DocumentRoot 包含在内.
如何/static在没有用户登录的情况下添加异常来提供应用程序的文件,但仍然将其before_request用于我的flask应用程序定义的路由?
我试图从棘轮实现push.js引擎:
http://maker.github.com/ratchet/#push
我从这里下载了棘轮文件:
http://maker.github.com/ratchet/ratchet.zip
并使用apache来服务所有js,css和html.所有文件都在同一目录中.
这是我的one.html文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ratchet template page</title>
<!-- Sets initial viewport load and disables zooming -->
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<!-- Include the compiled Ratchet CSS -->
<link rel="stylesheet" href="ratchet.css">
<!-- Include the compiled Ratchet JS -->
<script src="ratchet.js"></script>
</head>
<body>
<!-- Make sure all your bars are the first things in your <body> -->
<header class="bar-title">
<h1 class="title">one.html</h1>
</header>
<!-- Wrap all non-bar HTML in the .content div (this is …Run Code Online (Sandbox Code Playgroud) 如何获取d3.js数据映射支持的所有国家/地区的列表?
按照datamaps演示页面中的示例,我将此配置用于我的地图:
var basic = new Datamap({
element: document.getElementById("basic")
fills: {
defaultFill: "#ABDDA4",
authorHasTraveledTo: "#fa0fa0"
},
data: {
USA: { fillKey: "authorHasTraveledTo" },
JPN: { fillKey: "authorHasTraveledTo" },
ITA: { fillKey: "authorHasTraveledTo" },
CRI: { fillKey: "authorHasTraveledTo" },
KOR: { fillKey: "authorHasTraveledTo" },
DEU: { fillKey: "authorHasTraveledTo" },
}
});
Run Code Online (Sandbox Code Playgroud)
如何获取数据图中包含的所有国家/地区的列表(除了"USA","JPN","ITA"等)?
我想传递一个json对象,其中包含从我的客户端到我的服务器的嵌套对象.
在客户端,我的数据结构如下所示:
var response = {};
response['screening'] = '1';
response['assistance'] = 'wheelchair access';
response['guests'] = {};
response['guests']['1'] = {}
response['guests']['1']['first'] = 'John'
response['guests']['1']['last'] = 'Smith'
response['guests']['2'] = {}
response['guests']['2']['first'] = 'Dave'
response['guests']['2']['last'] = 'Smith'
Run Code Online (Sandbox Code Playgroud)
我的ajax调用看起来像这样:
$.ajax({
type: "POST",
url: window.location.pathname,
data: response
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
Run Code Online (Sandbox Code Playgroud)
在将此数据发布到使用python flask运行的服务器之后,我使用request.form对象来检查从客户端发布的内容.我想要以相同的方式构建数据,但是,这是服务器上的输出:
ImmutableMultiDict([('guests[1][first]', u'John'), ('screening', u'2'), ('guests[2][last]', u'Smith'), ('guests[2][first]', u'Dave'), ('assistance', u'wheelchair access'), ('guests[1][last]', u'Smith')])
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,响应['guests']对象变得平坦,并且它的所有子节点,例如:
的客人[2] [第一]'
......只是一个字符串,而不是父母反应的元素['来宾'].
有没有更好的方法将这个数据块从我的客户端发送到我的服务器,并保持其结构正确?
谢谢!
如何使用flask sqlalchemy将列定义为正整数?
我希望答案看起来像这样:
class City(db.Model):
id = db.Column(db.Integer, primary_key=True)
population = db.Column(db.Integer, positive=True)
def __init__(self,population):
self.population = population
Run Code Online (Sandbox Code Playgroud)
但是,这个类定义会抛出错误b/c sqlalchemy不知道'正面'参数.
如果对象被实例化为人口的负值,我可以引发异常.但我不知道如何确保更新后人口仍然是积极的.
谢谢你的帮助.
我正在使用Flask-RESTful开发API,而我的应用程序具有三个角色。
对于任何给定资源,根据每个角色,返回的JSON对象都有一组不同的键。
例如,如果您以“ site_admin”的身份打入/ orders,结果可能如下所示:
{
"orders": [
{"id": 1, "user": "foo", "paid": True, "department": "A", "code": 456},
{"id": 2, "user": "bar", "paid": False, "department": "A", "code": 567},
{"id": 3, "user": "meh", "paid": False, "department": "B", "code": 678}
]
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您以“ department_admin”身份打入/ orders,结果可能如下所示:
{
"orders": [
{"id": 3, "user": "meh", "paid": False}
]
}
Run Code Online (Sandbox Code Playgroud)
而且,如果您将/ orders设为“基本”,则将是一个非常小的JSON响应,如下所示:
{
"orders": [
{"id": 2, "paid": True}
]
}
Run Code Online (Sandbox Code Playgroud)
RESTful的实现方式是什么?
我可以提出三种方法。
(1)使用请求arg并对其进行过滤:
class Orders(restful.Resource):
def get(self):
if request.args['role'] == 'site_admin':
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试连接一个最小的概念验证来呈现单个 React 组件服务器端。
当我运行我的应用程序时,我收到一个错误:
SyntaxError: express.js: Unexpected token (10:41)
违规行是:
> 10 | res.send(ReactDOMServer.renderToString(<Component msg={msg} />));
| ^
Run Code Online (Sandbox Code Playgroud)
这是我的package.json:
{
"name": "ssrReact",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "nodemon express.js --exec babel-node --presets es2015,stage-2"
},
"dependencies": {
"express": "^4.14.1",
"react": "^15.4.2",
"react-dom": "^15.4.2"
},
"devDependencies": {
"babel-cli": "^6.22.2",
"babel-preset-es2015": "^6.22.0",
"babel-preset-stage-2": "^6.22.0",
"nodemon": "^1.11.0"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Component.js:
import React from 'react';
class Component extends React.Component {
render() {
return <h1>Hello, {this.props.msg}</h1>;
}
} …Run Code Online (Sandbox Code Playgroud) 在文档之后,我运行了以下命令:
aws ssm put-parameter --name foo --value bar --type SecureString
Run Code Online (Sandbox Code Playgroud)
我将此添加到我的serverless.yml:
custom:
foo: ${ssm:foo}
Run Code Online (Sandbox Code Playgroud)
部署时,我会收到以下警告:
Serverless Warning --------------------------------------
A valid SSM parameter to satisfy the declaration 'ssm:foo' could not be found.
Run Code Online (Sandbox Code Playgroud)
如何访问此变量?谢谢!
我正在循环一个事物列表,如果一个东西有一个图像我想要显示它.
但如果一件东西没有图像,我想显示默认文字:
<div ng-repeat="myThing in myThings>
<div ng-switch on="{{myThing.img}}" >
<span ng-switch-when="{{myThing.img}}">
<img src="/img/myThings/{{myThing.id}}" />
</span>
<span ng-switch-default>No Image</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这有效,并显示默认文本.
但是,浏览器也在尝试获取不存在的图像时抛出错误:
http://localhost:9000/img/myThings/%7B%7BmyThing.id%7D%7D 404 (Not Found)
Run Code Online (Sandbox Code Playgroud)
有没有办法格式化这个ng-switch函数,以便不抛出这个错误?