سؤال

On all my template rendering for a particular app, the output ends with None:

...</html>None

This must be a bug and probably in my code and I've spent days trying to track it down. There's nothing special about my app and this bug appears on every page I use template rendering, whether I use a seperate template engine or not. There is nothing special about my code:

    class Objectives(NewBaseHandler):

        @user_required
        def get(self):
            user = auth_models.User.get_by_id(long(self.auth.get_user_by_session()['user_id']))
            if user:
                self.render_template('objectives.html', {'user': user})
            else:

                self.render_template('/', {})

class NewBaseHandler(BaseHandler):

    """
........BaseHandler for all requests

........Holds the auth and session properties so they are reachable for all requests
...."""

    def dispatch(self):
        """
............Save the sessions for preservation across requests
........"""

        # self.session_store = sessions.get_store(request=self.request)
        # if self.request.host.find('localhost') > 0:  # for a Swedish domain that uses Swedish
        # or lang = os.environ.get("HTTP_ACCEPT_LANGUAGE")

        i18n.get_i18n().set_locale('sv')
        lang_code_get = self.request.get('hl', None)
        if lang_code_get:
           #self.session['i18n_language'] = lang_code_get
           i18n.get_i18n().set_locale(lang_code_get)
        try:
            response = super(NewBaseHandler, self).dispatch()
            self.response.write(response)
        finally:
            self.session_store.save_sessions(self.response)

    @webapp2.cached_property
    def auth(self):
        return auth.get_auth()

    @webapp2.cached_property
    def session_store(self):
        return sessions.get_store(request=self.request)

    @webapp2.cached_property
    def auth_config(self):
        """
............Dict to hold urls for login/logout
........"""

        return {'login_url': self.uri_for('login'),
                'logout_url': self.uri_for('logout')}

class BaseHandler(webapp2.RequestHandler):

    @webapp2.cached_property
    def jinja2(self):
        return jinja2.get_jinja2(app=self.app)

    def render_template(self, file, template_args):
        path = os.path.join(os.path.dirname(__file__), 'templates',
                            file)
        self.response.out.write(template.render(path, template_args))

    def render_jinja(self, filename, **template_args):
        self.response.write(self.jinja2.render_template(filename,
                            **template_args))

How can I check where the output None is coming from? It's probably not coming from the template and it doesn't seem to be coming from the handlers and there is no other code.

Thank you

هل كانت مفيدة؟

المحلول

In Objectives.get() you must return a value. Since you don't do this Python assumes the result is None. This value you get in NewBaseHandler.dispatch() when calling to base dispatch implementation and then write it to response.

If I get your app correctly returning empty string in get method will solve the problem.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top