开发者

Python web framework

Is there a Python web framework where the Python code and the HTML code are in the same file?

Django uses templates with a special language to generate HTML. Pylons uses a kind of language whose name is Mako.

I prefer to output HTML using calls like:

echo('''
    HTML string
''')

or

print '''
    HTML string
'''

instead of special languages.

Edit:

Can I do something like PHP?

echo( '''<p>Something</p>''' )
if cond:
    echo( '''<p>Another thing 1</p>''' )
else:
    echo( '''&开发者_JAVA技巧lt;p>Another thing 2</p>''' )


If you want to use print itself you may want to rely on cgi. Going for templates, the closest you can get is from webpy project's Templator

The web.py template language, called Templetor is designed to bring the power of Python to templates. Instead of inventing new syntax for templates, it re-uses python syntax. If you know Python programming language, you will be at home.


This might be what you prefer, but it's really against every best practice ever. That being said, you can do this with django.

from django.http import HttpResponse
dev some_view(request):
    return HttpResponse("This is foolish short term gain")


The web2py template engine allows you to use pure Python in your HTML templates (see details in the book). You can also use web2py's HTML helpers to build HTML in Python (this generates a server-side DOM that can be queried and manipulated before rendering to HTML). If desired, the template engine can be used independent of the framework (the relevant module is /gluon/template.py). If there's something in particular you want to do, feel free to ask on the mailing list.


Divmod Nevow will allow you do do that with either prints or a custom Python expression-based language that allows you to embed XHTML code directly into your scripts. However, it's beta-quality, based on Twisted and thus a bit unconventional, and the main site for the company that develops it is down, so I can't point you at a tutorial or anything right now. Use at your own peril.

The special syntax (called stan) syntax looks more or less like this:

tags.html[
    tags.head[ tags.title[ "Greetings!" ]],
    tags.body[
        tags.h1(style="font-size: large")[ "Now I will greet you:" ],
        tags.span[ "Hello, world" ]
    ]
]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜