1

我是 Google App Engine 和 Django 表单的新手。当我尝试运行以下代码时出现编码错误:

import webapp2
from google.appengine.ext import db
from google.appengine.ext.webapp import template

from django import newforms as forms
from google.appengine.ext.db import djangoforms

import os
import re

import fix_path
import config
import static

def slugify(s):
    return re.sub('[^a-zA-Z0-9-]+', '-', s).strip('-')

def format_post_path(post, num):
    slug = slugify(post.title)
    if num > 0:
        slug += "-" + str(num)
    return config.post_path_format % {
        'slug': slug,
        'year': post.published.year,
        'month': post.published.month,
        'day': post.published.day,
    }

def render_template(template_name, template_vals=None, theme=None):
    template_path = os.path.join("themes", theme or config.theme, template_name)
    return template.render(template_path, template_vals or {})

class BlogPost(db.Model):
    # The url path to the blog post. Posts have a path if they are published.
    path = db.StringProperty()
    title = db.StringProperty(required=True, Indexed=False)
    body = db.TextProperty(required=True)
    published = db.DateTimeProperty(auto_now_add=True)
    updated = db.DateTimeProperty(auto_now=True) 

def render(self):
    template_vals = {
        'config': config,
        'post': self,
    }
    return render_template("post.html", template_vals)

def publish(self):
    rendered = self.render()
    if not self.path:
        num = 0
        content = None
        while not content:
            path = format_post_path(self, num)
            content = static.add(path, rendered, "text/html")
            num += 1
        self.path = path
        self.put()
    else:
        static.set(self.path, rendered, "text/html")

class PostForm(djangoforms.ModelForm):
    class Meta:
        model = BlogPost
        exclude = ['path', 'published', 'update']

class PostHandler(webapp2.RequestHandler)
    def render_to_response(self, template_name, template_vals=None, theme=None):
        template_name = os.path.join("admin", template_name)
        self.response.out.write(render_template(template_name, template_vals, theme))

    def render_form(self, form):
        self.render_to_response("edit.html", {'form': form})

    def get(self):
        self.render_form(PostForm())

    def post(self)
        form = PostForm(date=self.request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.publish()
            self.render_to_response("published.html", {'post': post})
        else:
            self.render_form(form)

app = webapp2.WSGIApplication([('/admin/newpost', PostHandler)],
                              debug=True)

这是追溯。

    ERROR    2012-05-08 10:35:03,609 cgi.py:121] Traceback (most recent call last):
      File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 676, in Decorate
        return func(self, *args, **kwargs)
      File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 1911, in get_code
        source_code.decode(encoding)
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)

INFO     2012-05-08 10:35:03,627 dev_appserver.py:2891] "GET /admin/newpost HTTP/1.1" 500 -

回溯没有给我足够的信息来解决问题。有任何想法吗?

4

1 回答 1

1

当我遇到 appengine 的这个问题时,我可以将所有数据清理为 unicode / utf-8,然后它就可以工作了。

您的某些数据不是 unicode / utf-8。我在 GAE 的几个场合都遇到过这个问题,它总是数据而不是源代码在某种程度上不是 unicode。

如果您检查数据和/或源格式并将所有内容设置为 utf-8 / unicode,它将起作用。您似乎已经将所有内容都设置为 utf-8,但如果您仍然收到此错误,则表示某些字符串或类似的字符集是 utf-8 以外的其他字符集。

于 2012-05-08T13:22:53.387 回答