0

Cookie を確認し、その値を使用して、読み込むテンプレートを設定する必要があります。以下は、実際のコード スニペットです。

import webapp2 as webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import os

class genericPage(webapp.RequestHandler):
    def get(self):
        templatepath = os.path.dirname(__file__) + '/../templates/'
        ChkCookie = self.request.cookies.get("cookie")
        if ChkCookie == 'default':
            html = template.render(templatepath + 'default_header.html', {})
        else:
            html = template.render(templatepath + 'alt_header.html', {})
    self.response.out.write(html)

私の質問は、ChkCookieand if...else ステートメントを別のモジュールに移動して、上記のコードで呼び出す方法です。次のように:

# HOW I WANT TO MODIFY THE ABOVE CODE TO SET THE TEMPLATES WITH A COOKIE
import webapp2 as webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import os
from testmodule import testlibrary

class genericPage(webapp.RequestHandler):
    def get(self):
        html = testlibrary.ChkCookieClass.ChkCookie()
    self.response.out.write(html)

ChkCookie次のように、コードをgenericPageクラスに保持し、モジュールに関数のみが含まれている場合、ライブラリ/モジュールを正常にインポートできます。

# THIS IS THE MODULE I AM IMPORTING
import webapp2 as webapp
from google.appengine.ext.webapp import template
import os

def SkinChk(ChkCookie):
    templatepath = os.path.dirname(__file__) + '/../templates/'
    if ChkCookie == 'default':
        out = template.render(templatepath + 'default_header.html', {})
    else:
        out = template.render(templatepath + 'alt_header.html', {})
    return out

上記のモジュールコードを変更してそこに含めるにはどうすればよいChkCookie = self.request.cookies.get("cookie")ですか?

4

1 に答える 1