2017-10-15 1 views
-1

私はフォーラムという名前のデータベースに接続している簡単なポスト・アプリを書いた。私のコードは以下の通りです。Pythonの漂白ライブラリを使用してスクリプトの挿入を回避するにはどうすればよいですか?

# "Database code" for the DB Forum. 

import datetime, psycopg2, bleach 


POSTS = [("This is the first post.", datetime.datetime.now())] 

def add_post(message): 
    ''' This function insert post into the database ''' 
    db = psycopg2.connect(dbname = 'forum') 
    c = db.cursor() 
    c.execute("insert into posts values (%s)", (message,)) 
    db.commit() 
    db.close() 

def get_posts(): 
    '''Take the posts from the databse ''' 
    db = psycopg2.connect(dbname = 'forum') 
    c = db.cursor() 
    c.execute("select content, time from posts order by time") 
    result = c.fetchall() 
    POSTS.extend(result) 
    db.close() 
    return reversed(POSTS) 

このコードでは、漂白剤ライブラリを使用します。しかし、私はどのように知りません。私はすでに漂白剤のライブラリをインポートします。前もって感謝します。

text = bleach.clean(str(message)) 

そして、それに応じて実行する機能を調整します:

+0

あなたが求めていることは明確ではありません。あなたの質問が「どのように私は漂白剤ライブラリーを使用するのですか」というのは、現在の形では[SO]にはあまりにも広すぎて非特定です。 [ask]と[help/on-topic]を見てください。 – pvg

+0

Bleachは出力レイヤーに関連していて、ここのどちらのメソッドも実際に何も出力しません。 – MatsLindh

答えて

0

はあなたのadd_postメソッドで次の行を追加する必要が

c.execute("insert into posts values (%s)", (text,)) 

だから、完全なadd_post方法は、次のようになります。

def add_post(message): 
    ''' This function insert post into the database ''' 
    db = psycopg2.connect(dbname = 'forum') 
    c = db.cursor() 
    text = bleach.clean(str(message)) 
    c.execute("insert into posts values (%s)", (text,)) 
    db.commit() 
    db.close() 
関連する問題