如何为jinja2 template/GAE显示(转换)简单Python字典为字符串

2024-09-30 10:28:33 发布

您现在位置:Python中文网/ 问答频道 /正文

简单的问题,但大部分的搜索都没有得到一个完整的答案。我目前正在使用angularjs向数据存储(GAE)写入注释。但是我的页面将注释显示为字典而不是字符串(我需要实际输出)。我希望能够在jinja2模板中获取符合我需要的函数的注释。以下是我的档案。TIA公司

Python(主.py)

class expHandler(webapp2.RequestHandler):
def get(self):
    title="Colin_MK: Experience"
    recommendations = Recommendation.query()
    self.response.out.write(json.dumps([rec.to_dict() for rec in recommendations]))
    template_vars = {'title': title, 'recommendations': recommendations}
    template = JINJA_ENVIRONMENT.get_template('/exp.html')
    self.response.out.write(template.render(template_vars))

def post(self):
    r = json.loads(self.request.body)

    new_comment = Recommendation(comment=r['comment'])
    new_comment.put()

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/bio', bioHandler),
    ('/exp', expHandler),
], debug=True)

HTML(Jinja2模板)

^{pr2}$

角度(*获取零件)

var commentApp = angular.module('commentApp', [])

commentApp.config(['$interpolateProvider', function($interpolateProvider) {
$interpolateProvider.startSymbol('{<');
$interpolateProvider.endSymbol('>}');
}]);

commentApp.controller('commentController', function($scope, $http) {
    $scope.formFields = {
        comment : ""
    }

    $scope.comments = [];

    $scope.init = function(){

        $http({
            method: 'GET',
            url: '/exp'
            }).then(function successCallback(response) {

                $scope.comments = response;

            }, function errorCallback(response) {
                console.log(response);
            });

    };

    $scope.init();

Required display

What's currently displaying


Tags: self模板gettitleresponsedefcommentfunction

热门问题