Search code examples
pythonshellpyramid

Key error in request.params


I am a beginner in Python and I have to create a pyramid project that accepts user input from a from, and performs a simple operation that gives back the result to the user. This is my views.py

 from pyramid.response import Response
from pyramid.view import view_config
@view_config(route_name="home",renderer='templates/ui.pt')
def my_view(request):
    myvar=request.params['command']
    return Response(myvar)

This is my templates/ui.pt (excluding all initial html,head tags)

    <form action="my_view" method="POST"><input type="text" name="command"/>
<input type="submit" name="go" value="submit"/></form>
</html>

When I run this, I get this error Keyerror:'command'

Please help.


Solution

  • If no parameters are passed in your request (which is what would happen if you visit the page without posting to it or adding a param to the query string -- http://mypage/my_view?command=something), then the request.params MultiDict won't have a key named 'command' in it and that's where your error is coming from. You can either explicitly check if 'command' is in your request.params:

    myvar = None
    if 'command' in request.params:
        myvar = request.params['command']
    

    Or you can (more commonly) use the get method of a dictionary to supply a default value:

    myvar = request.params.get('command', None)
    

    Also since you're defining a template for your view, typically, your return value would be something that provides context to that template. Your code, however, isn't actually using the template but is instead directly returning a Response. You normally wouldn't do that. You'd do something more like this:

    @view_config(route_name="home",renderer='templates/ui.pt')
    def my_view(request):
        myvar=request.params.get('command',None)
        return {'myvar': myvar }
    

    And then in your template you'd reference the object it got passed:

    <!doctype html>
    <html>
    <body>
    <form method="POST"><input type="text" name="command"/>
    <input type="submit" name="go" value="submit"/></form>
    <div tal:condition="myvar">
       You entered <tal:block tal:content="myvar"></tal:block>
    </div>
    </body>
    </html>
    

    Here's a walkthrough from scratch to get the above working:

    Install pyramid:

    pip install pyramid
    

    Create your pyramid project:

    pcreate -t starter myproject
    

    Set up the environment for your project:

    cd myproject
    python setup.py develop
    

    Replace the myproject/views.py with:

    from pyramid.view import view_config
    
    @view_config(route_name="home",renderer='templates/ui.pt')
    def my_view(request):
        myvar=request.params.get('command',None)
        return {'myvar': myvar }
    

    Add the myproject/templates/ui.pt file:

    <!doctype html>
    <html>
    <body>
    <form method="POST"><input type="text" name="command"/>
    <input type="submit" name="go" value="submit"/></form>
    <div tal:condition="myvar">
       You entered <tal:block tal:content="myvar"></tal:block>
    </div>
    </body>
    </html>
    

    Start up your app:

    pserve --reload development.ini
    

    Visit your pyramid site:

    http://localhost:6543