我正在尝试使用webapp2对处理程序进行单元测试,并且遇到了一个只是一个愚蠢的小错误.
我希望能够在测试中使用webapp2.uri_for,但我似乎无法做到这一点:
def test_returns_200_on_home_page(self):
response = main.app.get_response(webapp2.uri_for('index'))
self.assertEqual(200, response.status_int)
Run Code Online (Sandbox Code Playgroud)
如果我这样做main.app.get_response('/')就行了.
收到的例外是:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 318, in run
testMethod()
File "tests.py", line 27, in test_returns_200_on_home_page
webapp2.uri_for('index')
File "/Users/.../webapp2_example/lib/webapp2.py", line 1671, in uri_for
return request.app.router.build(request, _name, args, kwargs)
File "/Users/.../webapp2_example/lib/webapp2_extras/local.py", line 173, in __getattr__
return getattr(self._get_current_object(), name)
File "/Users/.../webapp2_example/lib/webapp2_extras/local.py", line 136, in _get_current_object
raise RuntimeError('no object bound to %s' % self.__name__)
RuntimeError: no object bound to request
Run Code Online (Sandbox Code Playgroud)
我有什么愚蠢的设置吗?
我以前从未做过单元测试.我想掌握它.
我正在尝试测试我的webapp2处理程序.为此,我认为向处理程序发送请求是个好主意,例如:
request = webapp2.Request.blank('/')
# Get a response for that request.
response = request.get_response(main.app)
Run Code Online (Sandbox Code Playgroud)
问题是,响应主要只是一堆HTML等.
我想看看在处理程序转换为HTML之前从处理程序传递给我的jinja2模板的内容.
我希望我的测试能够处理处理程序类代码中的状态.我不想在响应处理程序中看到某些变量看起来像什么,然后我想看看dict模板在传递给render_to_response()之前的样子
我想测试这些变量是否具有正确的值.
这是我到目前为止的测试代码,但是因为response = request.get_response()只给了我一堆html而不是原始变量,所以我卡住了.
import unittest
#from google.appengine.ext import db
#from google.appengine.ext import testbed
#from google.appengine.datastore import datastore_stub_util
import main
import webapp2
class DemoTestCase(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def testNothing(self):
self.assertEqual(42, 21 + 21)
def testHomeHandler(self):
# Build a request object passing the URI path to be tested.
# You can also pass headers, query arguments etc.
request = webapp2.Request.blank('/') …Run Code Online (Sandbox Code Playgroud) 这将是很长的:
好的,我正在开发一个谷歌日历小工具,它将请求发送到托管在Google App Engine上的Python webapp2 REST api.
当我尝试发布因CORS而不允许我的东西时出现问题.在Chromes的DevTools中它说:
Method: OPTIONS.
Status: (failed) Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
Origin https://hq34i4geprnp5vci191ljfuhcoerscl4-a-calendar-opensocial.googleusercontent.com is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
我知道这是因为CORS.这里:
Ajax - Access-Control-Allow-Origin不允许"原始localhost"
它说我必须添加
Access-Control-Allow-Origin: *
Run Code Online (Sandbox Code Playgroud)
对于标题,但是我又是ajax的新手,我想知道它是否以这种方式完成:
$.ajax({
type: "POST",
url: "https://myapp.appspot.com/service",
contentType: "application/json; charset=utf-8",
data: data,
beforeSend: function (request)
{
request.setRequestHeader("Access-Control-Allow-Origin", "*");
}
success: function(data) {
alert("AJAX done");
}
});
Run Code Online (Sandbox Code Playgroud)
添加此标题的输出是不同的(这让我想知道原点是否被允许,但我真的不知道):
Method: OPTIONS.
Status: (failed) Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
XMLHttpRequest cannot load https://myapp.appspot.com/service. …Run Code Online (Sandbox Code Playgroud) 如何在以下示例中基于Python if语句添加条件CSS类以显示has-success has-feedback表单元素?
<div class="form-group {% if not error_username %} has-success has-feedback {% endif %}">
Run Code Online (Sandbox Code Playgroud) 在我的网络应用中,用户可以发布博客文章.当我显示博客文章时,不显示换行符,因为我没有用<br>标签替换新行.问题是我在Jinja中启用了autoescaping,因此<br>标签被转义.我不想暂时禁用autoescaping,我想特别允许<br>标签.我该怎么做?
我仍在收集信息,但似乎我无法弄清楚这两个框架的区别是什么.
目前我正在使用带有jinja2和wtforms的webapp2.它在GAE开箱即用,工作正常.
如果我比较烧瓶和webapp2,我只能看到烧瓶已经集成了jinja和wtforms,所以没什么大不了的.
如果我比较两个文档,似乎两个框架有很多共同点
http://webapp-improved.appspot.com/
只是有一些启动问题与烧瓶在我的Windows机器上本地运行.
至少烧瓶的文件看起来非常好.写得非常好,非常清楚.
我已经在网上搜索过,我找不到有关这些差异的任何信息.
适用于Google App Engine的Flask vs webapp2
那么,有什么区别?
我正在制作一个需要国家/地区短代码的网络应用程序(谷歌应用程序引擎从请求标题中获取)我希望获得国家/地区名称(全名),而不仅仅是2个字母的首字母缩写.
我尝试制作一个python字典,但它打破了bkz名称有非ascii字符(重音标记等)..我使用python库"pycountry"但我不知道如何在我的谷歌应用程序引擎项目中包含它.不幸的是,pycountries输出也有重音符号,所以我不能只复制他们的txt值并制作一本字典......
此外,我只想要国家代码命名查找表,没有其他细节......
这是我一直试图制作的词典的副本,但他们有这些恼人的重音符号......
我在这里先向您的帮助表示感谢
short2long = {"AF":"Afghanistan",
"AX":"Aland Islands",
"AL":"Albania",
"DZ":"Algeria",
"AS":"American Samoa",
"AD":"Andorra",
"AO":"Angola",
"AI":"Anguilla",
"AQ":"Antarctica",
"AG":"Antigua and Barbuda",
"AR":"Argentina",
"AM":"Armenia",
"AW":"Aruba",
"AU":"Australia",
"AT":"Austria",
"AZ":"Azerbaijan",
"BS":"Bahamas",
"BH":"Bahrain",
"BD":"Bangladesh",
"BB":"Barbados",
"BY":"Belarus",
"BE":"Belgium",
"BZ":"Belize",
"BJ":"Benin",
"BM":"Bermuda",
"BT":"Bhutan",
"BO":"Bolivia, Plurinational State of",
"BQ":"Bonaire, Sint Eustatius and Saba",
"BA":"Bosnia and Herzegovina",
"BW":"Botswana",
"BV":"Bouvet Island",
"BR":"Brazil",
"IO":"British Indian Ocean Territory",
"BN":"Brunei Darussalam",
"BG":"Bulgaria",
"BF":"Burkina Faso",
"BI":"Burundi",
"KH":"Cambodia",
"CM":"Cameroon",
"CA":"Canada",
"CV":"Cape Verde",
"KY":"Cayman Islands",
"CF":"Central African Republic",
"TD":"Chad",
"CL":"Chile",
"CN":"China",
"CX":"Christmas Island",
"CC":"Cocos (Keeling) …Run Code Online (Sandbox Code Playgroud) 我想访问给定请求的URI参数:
http://localhost:8080/account/user?un=erik&pw=gaius
Run Code Online (Sandbox Code Playgroud)
我不能使以下代码工作,
main.py
app = webapp2.WSGIApplication([('/', MainPage),
('/account/user', account.User)],
debug=True)
Run Code Online (Sandbox Code Playgroud)
account.py
class User(webapp2.RequestHandler):
def get(self, un, pw):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Yey!' + un + ' ' + pw)
Run Code Online (Sandbox Code Playgroud)
我认为我的main.py有问题,但我试图通过添加命名路由和正则表达式来解决它,但我只是得到500错误(内部服务器错误).
我是一名新手程序员,也是Google App Engine和webapp2等的新手.所以这可能是一个非常基本的问题.
我正在创建一个将图像存储到BlobStore的应用程序.我的模型存储描述,blob_key,图像URL和日期.
我能够保存所有东西,所以这一点都没关系.
但是现在我想创建一个删除按钮,它不仅会删除数据存储区中的项目,还会删除保存在blobstore中的图像.
我创建了一个DeleteHandler,并在html中有一个表单,传递我想要删除的项的键.在DeleteHandler中,我使用发布的密钥从数据存储中删除项目.我也在尝试使用密钥来删除保存在blobstore中的图像.
到目前为止,我在删除表单帖子上得到了404,即使我已经过去了,我也不确定我的DeleteHandler是否正确处理我正在寻找的功能.
任何帮助将非常感激..
Main.py:
import os
import urllib
import webapp2
from google.appengine.ext.webapp import template
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.api import images
#Models
from google.appengine.ext import db
class ImageItem(db.Model):
description = db.StringProperty(required=True)
img_url = db.StringProperty()
blob_key = blobstore.BlobReferenceProperty()
when = db.DateTimeProperty(auto_now_add=True)
#Handlers (Views)
class MainHandler(webapp2.RequestHandler):
def get(self):
upload_url = blobstore.create_upload_url('/upload')
imgs = db.GqlQuery(
'SELECT * FROM ImageItem '
'ORDER BY when DESC')
imgs_dict = {'imgs': imgs}
self.response.out.write( template.render( 'main.html',locals() ) …Run Code Online (Sandbox Code Playgroud) 如何创建初始化代码?当我把__init__构造函数总是告诉我参数是错误的.另外请举例说明使用__new__和使用super(),我们为什么要使用或不使用它们.
import webapp2
class MainHandler( webapp2.RequestHandler ):
def __init__( self ):
#initialization code/vars
x = 1
def get( self ):
#code for get here
self.response.write( x+1 )
def post( self ):
#code for post here
self.response.write( x+2 )
Run Code Online (Sandbox Code Playgroud)
app = webapp2.WSGIApplication([('/',MainHandler)],debug = True)