Question

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="you@domain.com">
        <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

Was it helpful?

Solution 2

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')

OTHER TIPS

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.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top