사용자 도구

사이트 도구


kb:django

Django

Python 기반 웹 서버 개발 프레임워크

샘플

일단은 완전한 것이 아니라, webapp2 프레임워크 상에서 템플릿만 이용하는 샘플

app.yaml

application: excel96
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: main.application

libraries:
  - name: webapp2
    version: latest
  - name: django
    version: latest

main.py

import os
import datetime
import webapp2
 
from django.template import Template, Context
from django.template.loader import get_template
from google.appengine.api import users
 
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
 
class MainPage(webapp2.RequestHandler):
    def get(self):
        current_time = datetime.datetime.now()
        user = users.get_current_user()
        login_url = users.create_login_url(self.request.path)
        logout_url = users.create_logout_url(self.request.path)
 
        t = get_template('main.html')
 
        c = Context({ 
            'current_time' : current_time, 
            'user' : user, 
            'login_url' : login_url, 
            'logout_url' : logout_url 
        })
 
        self.response.out.write(t.render(c)) 
 
application = webapp2.WSGIApplication([('/', MainPage)], debug=True)

settings.py

import os.path
 
DEBUG = True
TEMPLATE_DEBUG = DEBUG
 
TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'))

templates/main.html

<html>
<head>
<title>The Time Is...</title>
</head>
<body>
{% if user %}
<p>
Welcome, {{ user.email }}!
You can <a href="{{ logout_url }}">sign out</a>
{% else %}
Welcome!
<a href="{{ login_url }}">Sign in or register</a> to customize.
{% endif %}
<p>The time is: {{current_time}}</p>
</body>
</html>

링크

kb/django.txt · 마지막으로 수정됨: 2014/11/07 16:21 (바깥 편집)