python--web.py use

Source: Internet
Author: User

Web.py is a lightweight Python web framework.

I'll use the web.py framework to create a simple example of an HTML page.


1. Create a Web site root directory exweb2
mkdir EXWEB2


2. Create a virtual environment in the root directory of the Web site
CD EXWEB2
Virtualenv uniqueenv

3. Installing Flask
Uniqueenv/bin/pip Install Lpthw.web

4.web.py use, the app.py code is as follows

#-*-coding:utf-8-*-ImportWeb URLs=(        '/','Index') App=web.application (Urls,globals ())#Base as Base template pageRender = Web.template.render ('templates/', base="Base")classIndex (object):#Get method commits, if the URL parameter is not empty, jump directly to the display interface, otherwise jump to the input information interface        defGET (self): form= Web.input (name="", greet="")                ifform.greet!=""  andform.name!="": Greeting="%s,%s"%(Form.greet,form.name)returnRender.index (greeting=greeting)returnRender.hello_form ()#Post method to submit the form,        defPOST (self): form= Web.input (name="Nobody", greet="Hello") Greeting="%s,%s"%(Form.greet,form.name)returnRender.index (greeting=greeting)if __name__=="__main__": App.run ()

5. Then create the template page, and the template is placed under the Templates folder.

mkdir templates

First create a base template page base.html

$def with (content)                                                                              $:content        </body>

Enter information page hello_form.html

"/"Method="POST">A Greeting:<input type="text"Name="greet"><br/>Your Name:<input type="text"Name="name"><br/><input type="Submit"></form>

Display Information page index.html

$def  with (greeting)                                                             $if  greeting:         <em style="color: Green Font-size:2em; "> $greeting </em>. $Else:         <em>hello</em>, world!

7. The project directory structure is as follows:
Exweb2\
Uniqueenv\
app.py
Templates\

Base.html

Hello_form.html
Index.html

8. Run: Uniqueenv/bin/python app.py

Note that the Python interpreter in the virtual directory is used when running

9. Results:

(1) Get mode

(2) Post mode

python--web.py use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.