Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
13110 Lượt xem

Given the following URL: /route/?from=...?to=....

Could you please provide a code sample that demonstrates how to properly retrieve parameters from a query in an Odoo 16 controller?


class View(http.Controller):
@http.route('/smt', type='json', auth='public')
def ping(self, **kw):
print(kw) # kwargs is empty {}
return {'success': True}


Ảnh đại diện
Huỷ bỏ

did you already found what you were looking for?

Câu trả lời hay nhất

Sure, here's a code sample demonstrating how to properly retrieve parameters from a query in an Odoo 16 controller:

python Copy code from odoo import http

class  MyController (http.Controller):
     @http.route( '/route/' , type = 'http' , auth= 'public' , website= True ) 
    def  my_route ( self, **kwargs ):
        from_param = kwargs.get( 'from' )
        to_param = kwargs.get( 'to' )

        # Your processing logic here

        return  "From: {}, To: {}" . format (from_param, to_param)

In the code above, we define a controller class MyController with a route /route/ . When a request is made to this route with query parameters like /route/?from=...&to=... , the my_route method will be executed. The **kwargs parameter allows us to capture all the query parameters passed to the route.

We then use the kwargs.get() method to retrieve the specific query parameters we are interested in, namely from and to . The get() method returns None if the parameter is not present, or the parameter's value if it exists.

Finally, you can perform your processing logic with the from_param and to_param variables as needed. In the example provided, we simply return a formatted string displaying the values ​​of from_param and to_param . You can replace this with your actual processing and response logic.



Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

Suppose if url is as follows: /new_ticket?category_id=3 and controller as below

@http.route('/new_ticket', type='http', auth='public')
def ping(self, **kw):

Inside kw you can access the category_id, ie, kw.get('category_id') will return value as 3 in this case


Thanks & Regards

Walnut Software Solutions

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,
If your controller is type='json', then you need to pass the arguments via the route. If you are using rpc.query, you can use params to pass the arguments.

eg: rpc.query({ route: "/web/action/load",
                params: {
                    action_id: action_id,
                    additional_context: additional_context,
                },

or if you are using ajax, you can use call.

eg: 
ajax.jsonRpc('/get_schedule', 'call',{'date': date_selected,})

Or 

if you are using type='http', then you can pass the parameters with the link.
eg: /new_ticket?category_id=3


Then you'll get category_id in kw.

Hope it helps

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 9 24
2546
1
thg 4 24
2255
2
thg 1 20
7666
3
thg 7 25
7349
0
thg 2 23
971