user280960
user280960

Reputation: 721

Odoo 8 pass model to URL Controller

I am creating a redirect and passing object model to the parameter, but this does not work. Below is the code:

picking = http.request.env['stock.picking'].browse([2]) # get an object model
test = '/test/picking/' + slug(picking) # prepare url with slug
return http.local_redirect(test, {}) # redirect

Below is my redirected Route

@http.route('/test/picking/<model("stock.picking"):picking>', auth='user', methods=['GET', 'POST'], type='http')
def method_test(self, picking=None, **kw):
    print picking
    print "test"

but this gives me 404, route not found.

The log result is :

2016-12-20 20:14:18,990 19094 INFO NEW werkzeug: 127.0.0.1 - - [20/Dec/2016 20:14:18] "GET /test/picking/pg-000002-2 HTTP/1.1" 404 -

Upvotes: 1

Views: 1463

Answers (2)

Dachi Darchiashvili
Dachi Darchiashvili

Reputation: 769

I've not used slug before but I decide to help you. So I read some source code and made my own slug() working code.

from openerp.addons.website.models.website import slug
# some code here
@http.route('/crmlead/create', type='http', auth="user", website=True)
def create_crm_lead(self, **kwargs):
    crm_lead = request.env['crm.lead'].sudo().create({
        # arguments
    })
    return request.redirect("/crmlead/detail/%s" % slug(crm_lead))

@http.route('/crmlead/detail/<model("crm.lead"):lead>', type='http', auth="public", website=True)
def show_details_crm_lead(self, lead, **kwargs):
    import pdb; pdb.set_trace()
    pass

and in pdb:

2016-12-22 10:10:06,333 11747 INFO dec_21_01 werkzeug: 127.0.0.1 - - [22/Dec/2016 10:10:06] "GET /crmlead/create HTTP/1.1" 302 -
> /home/user/Git/controllers/main.py(60)show_details_crm_lead()
-> pass
(Pdb) l
57          @http.route('/crmlead/detail/<model("crm.lead"):lead>', type='http', auth="public", website=True)
58          def show_details_crm_lead(self, lead, **kwargs):
59              import pdb; pdb.set_trace()
60  ->          pass
61      
62          @http.route('/web/signup', type='http', auth='public', website=True)
(Pdb) print lead
crm.lead(2,)

So it works just that simple.

PS. I don't know reasons why your code doesn't works but just following my code will help you out. Maybe you are using different slug I've no idea, show us your imports

Upvotes: 0

Phillip Stack
Phillip Stack

Reputation: 3378

test = '/test/picking/{}'.format(slug(picking)) # prepare url with slug

Your route is listening for /test/.... but you are redirecting to test/...

Also ensure your controller sends a response to the client. Something like this.

return http.request.render('<your_addon>.<your_template_id>', data)

Upvotes: 0

Related Questions