pyramid 处理request中的参数

访问量: 862

refer to:  http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/narr/views.html

def myview(request):
    firstname = request.params['firstname']
    lastname = request.params['lastname']


<form method="POST" action="myview">
<div>
<input type="text" name="firstname"/>
</div>
<div>
<input type="text" name="lastname"/>
</div>
<input type="submit" value="Submit"/>
</form>


MVC, pass value from controller to view:

from pyramid.view import view_config

@view_config(renderer='string')
def hello_world(request):
    return {'content':'Hello!'}

render json:

@view_config(renderer='json')
def hello_world(request):
    return {'content':'Hello!'}

a restful example: ( http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/narr/viewconfig.html)

from pyramid.view import view_defaults
from pyramid.view import view_config
from pyramid.response import Response

@view_defaults(route_name='rest')
class RESTView(object):
    def __init__(self, request):
        self.request = request

    @view_config(request_method='GET')
    def get(self):
        return Response('get')

    @view_config(request_method='POST')
    def post(self):
        return Response('post')

    @view_config(request_method='DELETE')
    def delete(self):
        return Response('delete')

# 渲染JSON 啥的。

from pyramid.view import view_config

@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
    return {'project': 'test_pyramid_starter222', 'foo2': '33'}

@view_config(route_name='recommended_applications', renderer='templates/recommended_applications.pt')
def recommended_applications(request):
    return {'var1': 1, 'var2': 2}

@view_config(route_name='test_json', renderer='json')
def test_json(request):
    return {'action' : 'say', 'value' : 'hi' }

for tabs:

request.matchdict['id']

订阅/RSS Feed

Subscribe

分类/category