2011-06-30 12 views
8

cherrypyの基本的なコンセプトに問題がありますが、私はチュートリアルやこれを行う方法の例が見つかりませんでした(私はCherrypy初心者です) 。Cherrypyの静的なhtmlファイル

問題。 (これはテストピースなので、認証とセッションはコードでは堅牢ではありません)

ユーザはログインページであるindex.htmlページに移動し、詳細が一致しない場合エラーメッセージが返され、表示されます。これは動作します! 詳細が正しければ、別のhtmlファイルがユーザに表示されます(network.html)これは私が働くことができないビットです。

現在のファイルシステムは次のようになります。 - :(私はどこにコメントを配置ファイルのレイアウトは、私は、コードは次のようになりindex.htmlを にアクセスできるように、右のようです

AppFolder 
    - main.py (main CherryPy file) 
    - media (folder) 
     - css (folder) 
     - js (folder) 
     - index.html 
     - network.html 

私は

import cherrypy 
import webbrowser 
import os 
import simplejson 
import sys 

from backendSystem.database.authentication import SiteAuth 

MEDIA_DIR = os.path.join(os.path.abspath("."), u"media") 

class LoginPage(object): 
@cherrypy.expose 
def index(self): 
    return open(os.path.join(MEDIA_DIR, u'index.html')) 

@cherrypy.expose 
def request(self, username, password): 
    print "Debug" 
    auth = SiteAuth() 
    print password 
    if not auth.isAuthorizedUser(username,password): 
     cherrypy.response.headers['Content-Type'] = 'application/json' 
     return simplejson.dumps(dict(response ="Invalid username and/or password")) 
    else: 
     print "Debug 3" 
     #return network.html here 

class DevicePage(object): 
@cherrypy.expose 
def index(self): 
    return open(os.path.join(MEDIA_DIR, u'network.html')) 


config = {'/media': {'tools.staticdir.on': True, 'tools.staticdir.dir': MEDIA_DIR, }} 

root = LoginPage() 
root.network = DevicePage() 

# DEVELOPMENT ONLY: Forces the browser to startup, easier for development 
def open_page(): 
webbrowser.open("http://127.0.0.1:8080/") 
cherrypy.engine.subscribe('start', open_page) 

cherrypy.tree.mount(root, '/', config = config) 
cherrypy.engine.start() 

この問題で任意の助けや指導をいただければ幸いです)新しいページを返すようにしようとしています

乾杯

クリス

答えて

5

基本的に2つのオプションがあります。ユーザーが/requestを訪問し、戻ってそのnetwork.htmlコンテンツを取得したい場合は、単にそれを返す:

class LoginPage(object): 
    ... 
    @cherrypy.expose 
    def request(self, username, password): 
     auth = SiteAuth() 
     if not auth.isAuthorizedUser(username,password): 
      cherrypy.response.headers['Content-Type'] = 'application/json' 
      return simplejson.dumps(dict(response ="Invalid username and/or password")) 
     else: 
      return open(os.path.join(MEDIA_DIR, u'network.html')) 

他のアプローチは、許可、コンテンツにリダイレクトする場合/requestとを、訪問するユーザーのためになります別のURL、おそらく/deviceで:

class LoginPage(object): 
    ... 
    @cherrypy.expose 
    def request(self, username, password): 
     auth = SiteAuth() 
     if not auth.isAuthorizedUser(username,password): 
      cherrypy.response.headers['Content-Type'] = 'application/json' 
      return simplejson.dumps(dict(response ="Invalid username and/or password")) 
     else: 
      raise cherrypy.HTTPRedirect('/device') 

彼らのブラウザは、新しいリソースのための第2の要求を行います。

+0

アドバイスをいただきありがとうございました。ありがとうございました。 – Lipwig

関連する問題