Juzzbott
Juzzbott

Reputation: 1789

Pyramid self.request.POST is empty - no post data available

I'm currently working on a pyramid project, however I can't seem to submit POST data to the app from a form.

I've created a basic form such as:


    <form method="post" role="form" action="/account/register">
    <div class="form-group">
        <label for="email">Email address:</label>
        <input type="email" class="form-control" id="email" placeholder="[email protected]">
        <p class="help-block">Your email address will be used as your username</p>
    </div>

    <!-- Other items removed -->
</form>

and I have the following route config defined:


    # projects __init__.py file
config.add_route('account', '/account/{action}', request_method='GET')
config.add_route('account_post', '/account/{action}', request_method='POST')

# inside my views file
@view_config(route_name='account', match_param="action=register", request_method='GET', renderer='templates/account/register.jinja2')
def register(self):
    return {'project': 'Register'}

@view_config(route_name='account_post', match_param="action=register", request_method='POST', renderer='templates/account/register.jinja2')
def register_POST(self):
    return {'project': 'Register_POST'}

Now, using the debugger in PyCharm as well as the debug button in pyramid, I've confirmed that the initial GET request to view the form is being processed by the register method, and when I hit the submit button the POST request is processed by the *register_POST* method.

However, my problem is that debugging from within the *register_POST* method, the self.request.POST dict is empty. Also, when I check the debug button on the page, the POST request is registered in the list, but the POST data is empty.

Am I missing something, or is there some other way of access POST data?

Cheers, Justin

Upvotes: 1

Views: 1940

Answers (3)

Juzzbott
Juzzbott

Reputation: 1789

I've managed to get it working. Silly me, coming from an ASP.NET background forgot the basics of POST form submissions, and that's each form field needs a name== attribute. As soon as I put them in, everything started working.

Upvotes: 2

solusipse
solusipse

Reputation: 587

That does nothing, I belive.

return {'project': 'Register_POST'}

POST parameters are stored inside request, so you have to do something like this.

def register_POST(self, request):
     return {'project': request.POST}

To access email input (which has to be named, for example: name="email"), use get() method:

request.POST.get('email')

Upvotes: 1

Anvesh
Anvesh

Reputation: 625

   <form method="post" role="form" action="/account/register"> {% csrf_token %}

Try using "csrf token". hope it works. remaining code looks fine.

Upvotes: 0

Related Questions