由于%s
我的应用程序出错了(至少,这是我的想法),但我不知道为什么。我已经改为django代码,试图将数据传递给html模板。模板正在加载但未导入数据。该应用程序确实在本地运行,但模板没有加载数据,在服务器上我收到此错误:
Traceback (most recent call last):
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~ceemee11111/1.363684484611202021/helloworld.py", line 37, in get
for greeting in greetings:
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2314, in next
return self.__model_class.from_entity(self.__iterator.next())
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 1442, in from_entity
return cls(None, _from_entity=entity, **entity_values)
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 974, in __init__
prop.__set__(self, value)
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 614, in __set__
value = self.validate(value)
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2823, in validate
raise BadValueError('Property %s is not multi-line' % self.name)
BadValueError: Property content is not multi-line
helloworld.py:
import cgi
import datetime
import urllib
import webapp2
import os
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext.webapp import template
class Greeting(db.Model):
"""Models an individual Guestbook entry with an author, content, and date."""
author = db.StringProperty()
content = db.StringProperty(multiline=False)
content2 = db.StringProperty(multiline=False)
date = db.DateTimeProperty(auto_now_add=True)
def guestbook_key(guestbook_name=None):
"""Constructs a Datastore key for a Guestbook entity with guestbook_name."""
return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook')
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write('<html><body>')
guestbook_name=self.request.get('guestbook_name')
greetings = db.GqlQuery("SELECT * "
"FROM Greeting "
"WHERE ANCESTOR IS :1 "
"ORDER BY date DESC LIMIT 10",
guestbook_key(guestbook_name))
for greeting in greetings:
if greeting.author:
self.response.out.write(
'<b>%s</b> wrote:' % greeting.author)
else:
self.response.out.write('An anonymous person wrote:')
self.response.out.write(template.render('myhtml.html', {'guestbook_name': guestbook_name}))
class Guestbook(webapp2.RequestHandler):
def post(self):
guestbook_name = self.request.get('guestbook_name')
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = users.get_current_user().nickname()
greeting.content = self.request.get('content')
greeting.put()
self.redirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name}))
app = webapp2.WSGIApplication([('/', MainPage),
('/sign', Guestbook)],
debug=True)
myhtml.html:
<!DOCTYPE html>
<html>
<body>
<form action="/sign?{{ guestbook_name }}" method="post">
<div id="dataImput">
<div><div><input type="text" name="content"</div>
<div><div><input type="text" name="content2"</div>
</div>
<script>
document.write("<h1>This is heading</h1>");
</script>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
<form>Guestbook name: <input value="{{ guestbook_name|escape }}" name="guestbook_name">
<input type="submit" value="switch"></form>
<hr>
</body>
答案 0 :(得分:1)
我对谷歌应用引擎一无所知(但我知道django)。您在模型中使用的类型与模板中使用的表单类型之间似乎存在冲突:
class StringProperty(verbose_name=None, multiline=False, ...)
如果multiline为False,则该值不能包含换行符。 djangoforms库使用它来强制文本之间的差异 数据模型中的字段和textarea字段,其他人可以使用它 出于类似的目的。
回溯的实际错误行是:
BadValueError: Property content is not multi-line
“content”是指您的模型属性。
对我而言,表明您需要执行以下任一选项...
1)更改模型以接受从textarea表单字段传递的多行值:
class Greeting(db.Model):
...
content = db.StringProperty(multiline=True)
content2 = db.StringProperty(multiline=True)
...
2)或者将您的textarea表单字段更改为模板中的文本字段:
<div><input type="text" name="content"></div>
<div><input type="text" name="content2"></div>
答案 1 :(得分:0)
我不确切知道我是如何解决这个问题的,但是这个代码的工作原理就像错误一样。我发布这个作为%s错误的信息只是因为模板仍然不起作用
丹
import cgi
import datetime
import urllib
import webapp2
import os
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext.webapp import template
class Greeting(db.Model):
"""Models an individual Guestbook entry with an author, content, and date."""
author = db.StringProperty()
content = db.StringProperty(multiline=True)
content2 = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
def guestbook_key(guestbook_name=None):
"""Constructs a Datastore key for a Guestbook entity with guestbook_name."""
return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook')
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write('<html><body>')
guestbook_name=self.request.get('guestbook_name')
greetings = db.GqlQuery("SELECT * "
"FROM Greeting "
"WHERE ANCESTOR IS :1 "
"ORDER BY date DESC LIMIT 10",
guestbook_key(guestbook_name))
for greeting in greetings:
if greeting.author:
self.response.out.write(greeting.author)
else:
self.response.out.write('An anonymous person wrote:')
self.response.out.write(template.render('myhtml.html', {'guestbook_name': guestbook_name}))
class Guestbook(webapp2.RequestHandler):
def post(self):
guestbook_name = self.request.get('guestbook_name')
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = users.get_current_user().nickname()
greeting.content = self.request.get('content')
greeting.put()
self.redirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name}))
app = webapp2.WSGIApplication([('/', MainPage),
('/sign', Guestbook)],
debug=True)
myhtml.html代码:
<!DOCTYPE html>
<html>
<body>
<form action="/sign?%s" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
<script>
document.write("<h1>This is heading</h1>");
</script>
</body>
</html>
答案 2 :(得分:0)
这是用于加载模板的代码(仅供参考)。感谢大家让我在Django / Python编码中找到了这一点。没有你就无法做到!
import cgi
import datetime
import urllib
import webapp2
import os
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext.webapp import template
class Greeting(db.Model):
"""Models an individual Guestbook entry with an author, content, and date."""
author = db.StringProperty()
content = db.StringProperty(multiline=True)
content2 = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
def guestbook_key(guestbook_name=None):
"""Constructs a Datastore key for a Guestbook entity with guestbook_name."""
return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook')
class MainPage(webapp2.RequestHandler):
def get(self):
guestbook_name=self.request.get('guestbook_name')
greetings_query = Greeting.all().ancestor(
guestbook_key(guestbook_name)).order('-date')
greetings = greetings_query.fetch(3)
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
template_values = {
'greetings': greetings,
'url': url,
'url_linktext': url_linktext,
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
class Guestbook(webapp2.RequestHandler):
def post(self):
guestbook_name = self.request.get('guestbook_name')
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = users.get_current_user().nickname()
greeting.content = self.request.get('content')
greeting.put()
self.redirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name}))
app = webapp2.WSGIApplication([('/', MainPage),
('/sign', Guestbook)],
debug=True)
和index.html代码:
<html>
<body>
{% for greeting in greetings %}
{% if greeting.author %}
<b>{{ greeting.author }}</b> wrote:
{% else %}
An anonymous person wrote:
{% endif %}
<blockquote>{{ greeting.content|escape }}</blockquote>
{% endfor %}
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
<a href="{{ url }}">{{ url_linktext }}</a>
</body>
答案 3 :(得分:0)
我有同样的问题,改变这些行:
content = db.TextProperty()
content2 = db.TextProperty()
对于那些:
{{1}}