3

我的应用程序因为%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

你好世界.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)

我的html.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>

4

4 回答 4

1

我对谷歌应用引擎一无所知(但我知道 django)。您在模型中使用的类型与您在模板中使用的表单类型之间似乎存在冲突:

类 StringProperty(verbose_name=None, multiline=False, ...)

如果 multiline 为 False,则该值不能包含换行符。djangoforms 库使用它来强制区分数据模型中的文本字段和 textarea 字段,其他人可以将其用于类似目的。

回溯中重要的实际错误行是:

BadValueError: Property content is not multi-line

“内容”是指您的模型属性。

对我来说,这表明您需要执行以下选项之一...

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>
于 2012-12-06T22:16:14.253 回答
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>

于 2012-12-07T21:16:24.980 回答
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>
于 2012-12-07T19:54:05.997 回答
0

我有同样的问题,改变这些行:

content = db.StringProperty(multiline=True)
content2 = db.StringProperty(multiline=True)

对于那些:

content = db.TextProperty()
content2 = db.TextProperty()
于 2016-08-15T07:14:27.507 回答