[Twisted-web] JSON "page" and nevow

Vincent Bernat bernat at luffy.cx
Mon Aug 18 15:14:39 EDT 2008


OoO En ce début d'après-midi nuageux du samedi 16 août 2008, vers 14:50,
Jean-Paul Calderone <exarkun at divmod.com> disait :

> JSON isn't XHTML, and the `docFactory´ and `render_*´ mechanisms are
> primarily concerned with the generation of XHTML.  To generate JSON,
> I think you'd do better to ignore these features of Page and override
> the `renderHTTP´ method instead.  You won't have to deal with < being
> turned into &lt; and other such XHTML-related problems.

Thanks for the tip.

I have added the following function:

,----
|     addSlash = True
| 
|     def renderHTTP(self, ctx):
|         request = inevow.IRequest(ctx)
|         if inevow.ICurrentSegments(ctx)[-1] != '':
|             request.redirect(request.URLPath().child(''))
|             return ''
|         request.setHeader("Content-Type",
|                           "application/json; charset=UTF-8")
|         d = defer.maybeDeferred(self.data_json, ctx, None)
|         d.addCallback(lambda x: self.render_json(ctx, x))
|         return d
`----

This now works fine.

Thanks for your help.
-- 
Test input for validity and plausibility.
            - The Elements of Programming Style (Kernighan & Plauger)



More information about the Twisted-web mailing list