在金字塔中,如何从视图返回原始HTML? [英] In Pyramid, how do I return raw HTML from a view?

查看:79
本文介绍了在金字塔中,如何从视图返回原始HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是Pyramid的新手(一般来说对Web框架来说还很新).

I'm really new to Pyramid (and pretty new to web frameworks in general).

我正在努力达到可以从视图返回原始HTML的阶段,以便可以标记从mongoDB存储返回的数据.

I'm trying to get to the stage where I can return raw HTML from a view, so that I can markup data returned from my mongoDB store.

我在金字塔项目中的__init__.py是标准的:

My __init__.py in my pyramid project is standard:

def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
config = Configurator(root_factory = Root, settings = settings)
config.add_view('hermesweb.views.my_view',
                context = 'hermesweb:resources.Root',
                renderer = 'hermesweb:templates/mytemplate.pt')
config.add_static_view('static', 'hermesweb:static', cache_max_age = 3600)
views.myDB = connect() # connect to my mongoDB

我的templates/mytemplate.pt看起来像这样:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:tal="http://xml.zope.org/namespaces/tal">
<head><title>My test title. . . </title></head>
<body>
    <div>
        <h2>Perform a search</h2>
        <form method="GET" action="">
            <div>
                <input type="text" name="id"/>
            </div>
            <input type="submit" value="Submit"/>
        </form>
        <h2>Results</h2>
        ${results}
    </div>
</body
<html>

最后,我的views.py看起来像这样:

Finally, my views.py looks like this:

myDB = "" # ref to the database is assigned on startup.
def my_view(request):
    key = request.GET.get('id', None)
    results = ""
    if key:
        db_res = myDB.call_some_find_function(key)
        for data in db_res:
            results = "%s <li> %s </li>" % (results, data)
        results = "<ul> %s </ul>" % results

    return {'results': results}

当我在表单中插入一个术语并调用my_view函数时,将查询数据库并提取正确的结果,但是,不是将返回的字符串变成网页中的html,而是将其打印为而是在网页中输入一个字符串.

When I insert a term into the form and the my_view function gets called the database is queried and the correct results get pulled out, however, rather than the string being returned turning into html in the webpage, it is printed as a string in the web-page instead.

我怀疑这与内容类型有关吗?但是我还不太了解金字塔.有人可以解释一下如何使它返回被浏览器解释为html而不是字符串的html吗?

I suspect this is something to do with the content type? But I don't really understand Pyramid well enough yet. Can someone explain how to get this to return html that is interpreted by the browser as html, rather than just a string?

其他问题-这种类型的数据库调用是否应该甚至使用views.py?我仍然对整个Root对象进入的位置感到困惑.我正在使用MongoDB作为数据库后端. .

Extra question - should I be even using the views.py for this type of database call? I'm still confused where the whole Root object comes into it. I'm using MongoDB as the database backend. . .

推荐答案

要防止Chameleon转义${result}变量,您需要根据文档使用${structure: result}:

To prevent Chameleon from escaping the ${result} variable, you need to use ${structure: result}, as per the documentation: http://chameleon.readthedocs.org/en/latest/reference.html#structure

这篇关于在金字塔中,如何从视图返回原始HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆