如何在Google App Engine中修复此编码错误

时间:2012-05-08 10:46:21

标签: google-app-engine django-forms

我是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 -

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

1 个答案:

答案 0 :(得分:1)

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

您的部分数据不是unicode / utf-8。我在使用GAE的几个场景中遇到过这个问题,它始终是数据,而不是源代码在某种程度上不是unicode。

如果您检查数据和/或源格式并将所有内容设置为utf-8 / unicode,它将起作用。看起来你已经把所有内容设置为utf-8,但是如果你仍然得到这个错误,那就意味着某些字符串或类似字符串是比utf-8更多的其他字符集。