Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does web.py get the parameters of a get request

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how web.py gets the parameters of get request". In the daily operation, I believe that many people have doubts about how web.py obtains the parameters of get request. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how web.py gets the parameters of get request". Next, please follow the editor to study!

Parameters of the GET request

When the browser makes a request to the server, it can carry parameters, and the server program will return what it needs according to these parameters, such as the following url: http://www.zhangdongshengtech.com/?page=2

Pay attention to page=2, this is the way to write get request parameters.

To? Start

The latter parameter form is key=value

If you have more than one parameter, split with &

The above get request parameter is page=2, and the request is the content of page 2 of my blog post. Next, let's learn how to get the parameters of the get request in the web.py framework.

Three steps are effective.

Based on the sample code in lesson 4, follow my steps to implement it step by step

1. Add a pair of url mappings

'/ hello','hello'

2. Add a hello class, defined as follows

Class hello:

Def GET (self):

I = web.input (name=None)

Return i.name

3. Launch server and enter the URL http://localhost:8080/hello?name= in the browser

In the browser, you will see the words "rookie world", which indicates that the parameters of the get request have been obtained correctly. Web.input, which is used to get the parameters of the get request. Name=None in parentheses means that if the get request does not take a parameter, then the value of name defaults to None.

First acquaintance template

It's not difficult to just get the parameters. In Lecture 4, you have learned how to use the web page. What if you want to get the parameter information in the web page?

Follow my steps.

1. Add a new web page called hello.html to templates with the following contents

$def with (name)

Hello

Hello $name

2. Modify the return statement in class hello

Return render.hello (i.name)

3. Now, type http://localhost:8080/hello?name= rookie world in the browser, and what you should see in the browser is "hello rookie world".

In the hello.html page I just wrote, there is no word "rookie world" at all, but when the page is displayed, notice that return render.hello (i.name) this sentence, I put the parameters into the hello method, here is the template technology.

Three key technical points

To understand template technology, first sort out a few technical points:

The first line of the hello.html page is $def with (name), which is associated with return render.hello (i.name). The contents in these two parentheses correspond to each other. Their names do not have any relationship, and it is their position that forms the corresponding relationship.

If return render.hello is used in the py program, then the page should be defined as $def with (name,age). The variable name can be picked up at will, but the parameters should correspond one by one.

The $name representation here is replaced with the name value passed in the first line, so that the content behind the hello can be changed according to the response of the server.

At this point, the study on "how to get the parameters of the get request by web.py" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report