TurboGears-HTTPメソッド

Httpプロトコルは、ワールドワイドウェブでのデータ通信の基盤です。このプロトコルでは、指定されたURLからデータを取得するさまざまな方法が定義されています。次の表は、さまざまなhttpメソッドをまとめたものです。

シニア番号 HTTPメソッドと説明
1

GET

暗号化されていない形式でデータをサーバーに送信します。最も一般的な方法。

2

HEAD

GETと同じですが、応答本文はありません

3

POST

HTMLフォームデータをサーバーに送信するために使用されます。POSTメソッドで受信したデータはサーバーにキャッシュされません。

4

PUT

ターゲットリソースの現在のすべての表現をアップロードされたコンテンツに置き換えます。

5

DELETE

URLで指定されたターゲットリソースの現在の表現をすべて削除します

HTMLフォームの作成

HTMLフォームを作成し、フォームデータをURLに送信しましょう。次のスクリプトをlogin.htmlとして保存します

<html>
   <body>
      <form action = "http://localhost:8080/login" method = "get">
         <p>Enter Name:</p>
         <p><input type = "text" name = "nm" /></p>
         <p><input type = "submit" value = "submit" /></p>
      </form>
   </body>
</html>

このフォームに入力されたデータは、 ‘/login’ URL。次に、コントローラー関数を作成しますloginpage() 上記のhtmlページを公開します。

@expose("hello.templates.login")
   def loginpage(self):
      return {}

フォームデータを受信するには、 login()コントローラ。パラメータとしてフォーム属性があります。ここに‘nm’ ログインフォームのテキスト入力フィールドの名前であり、login()関数のパラメータとして使用されます。

@expose("hello.templates.sample")
   def login(self, nm):
      name = nm
      return {'person':name}

ご覧のとおり、ログインフォームから受信したデータはsample.htmlテンプレート(先ほど使用)に送信されています。それはによって解析されますGenshi template engine 次の出力を生成するには-

POSTメソッド

HTMLフォームがPOSTメソッドを使用してアクション属性のURLにデータをディスパッチする場合、フォームデータはURLで公開されません。エンコードされたデータは、dictコントローラ関数による引数。**kw 以下の引数は、データを保持する辞書オブジェクトです。

HTMLフォームには、2つの入力テキストフィールドが含まれています。

<html>
   <body>
	
      <form action = "http://localhost:8080/marks" method = "post">
         <p>Marks in Physics:</p>
         <p><input type = "text" name = "phy" /></p>
         <p>Marks in Maths:</p>
         <p><input type = "text" name = "maths" /></p>
         <p><input type = "submit" value = "submit" /></p>
      </form>
		
   </body>	
</html>

ザ・ marks() コントローラはフォームデータを受信し、に送信します sample.htmlテンプレート。のコードroot.py 次のとおりです-

from hello.lib.base import BaseController
from tg import expose, request

class RootController(BaseController):
   @expose("hello.templates.marks")
   def marksform(self):
      return {}
		
   @expose("hello.templates.sample")
   def marks(self, **kw):
      phy = kw['phy']
      maths = kw['maths']
      ttl = int(phy)+int(maths)
      mydata = {'phy':phy, 'maths':maths, 'total':ttl}
      return mydata

最後に、sample.htmlテンプレートは次のとおりです-

<html>
   <head>
      <title>TurboGears Templating Example</title>
   </head>
	
   <body>
      <h2>Hello, Welcome to TurboGears!.</h2>
      <h3>Marks in Physics: ${phy}.</h3>
      <h3>Marks in Maths: ${maths}.</h3>
      <h3>Total Marks: ${total}</h3>
   </body>
	
</html>

サーバーを起動します(まだ実行されていない場合)

Gearbox server –reload –debug

入る http://localhost::8080/marksform ブラウザで

ザ・ sample.html 次の出力をレンダリングします-