Permalink
Browse files

add example

  • Loading branch information...
1 parent 598dc12 commit 62e263bb507e86466ec548de60b8a4372ddaa77a lepture committed Feb 11, 2012
View
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+
+import os
+
+rootdir = os.path.abspath(os.path.dirname(__file__))
+import site
+projdir = os.path.split(rootdir)[0]
+site.addsitedir(projdir)
+
+import tornado.options
+import tornado.locale
+from tornado.options import define, options
+from tornado.ioloop import IOLoop
+from tornado.httpserver import HTTPServer
+from tornado import web
+os.environ['TZ'] = 'UTC'
+
+define('port', default=8000, type=int)
+
+
+class Application(web.Application):
+ def __init__(self):
+ from handlers import handlers
+ settings = dict(
+ debug=True,
+ autoescape=None,
+ template_path=os.path.join(rootdir, "templates"),
+ static_path=os.path.join(projdir, "static"),
+ )
+ super(Application, self).__init__(handlers, **settings)
+ tornado.locale.load_translations(os.path.join(rootdir, "locale"))
+ #tornado.locale.set_default_locale('zh_CN')
+
+
+def run_server():
+ tornado.options.parse_command_line()
+ server = HTTPServer(Application(), xheaders=True)
+ server.bind(int(options.port))
+ server.start(1)
+ IOLoop.instance().start()
+
+
+if __name__ == "__main__":
+ run_server()
View
@@ -0,0 +1,43 @@
+from tforms import validators
+from tforms.fields import TextField, PasswordField
+from tforms.forms import TornadoForm as Form
+from tforms.validators import ValidationError
+
+
+class SignupForm(Form):
+ username = TextField(
+ 'Username', [
+ validators.Required(),
+ validators.Length(min=4, max=16),
+ validators.Regexp(
+ '[a-zA-Z0-9-]',
+ message='Username can only contain characters and digits',
+ ),
+ ],
+ description='Username can only contain English characters and digits'
+ )
+ email = TextField(
+ 'Email', [
+ validators.Required(),
+ validators.Length(min=4, max=30),
+ validators.Email(),
+ ],
+ description='Please active your account after registration'
+ )
+ password = PasswordField(
+ 'Password', [
+ validators.Required(),
+ validators.Length(min=6),
+ ],
+ description='Password must be longer than 6 characters'
+ )
+ password2 = PasswordField(
+ 'Confirm', [
+ validators.Required(),
+ validators.Length(min=6),
+ ]
+ )
+
+ def validate_password(form, field):
+ if field.data != form.password2.data:
+ raise ValidationError("Passwords don't match")
@@ -0,0 +1,34 @@
+import tornado.web
+import logging
+from tornado.options import options
+from forms import SignupForm
+
+
+class BaseHandler(tornado.web.RequestHandler):
+ _first_running = True
+
+ def initialize(self):
+ if BaseHandler._first_running:
+ logging.info('First Running')
+ BaseHandler._first_running = False
+
+ def prepare(self):
+ options.tforms_locale = self.locale
+
+
+class SignupHandler(BaseHandler):
+ def get(self):
+ form = SignupForm()
+ self.render('signup.html', form=form)
+
+ def post(self):
+ form = SignupForm(self.request.arguments)
+ if form.validate():
+ self.write('validate')
+ return
+ self.render('signup.html', form=form)
+
+
+handlers = [
+ ('/', SignupHandler),
+]
@@ -0,0 +1,32 @@
+# tforms
+"Not a valid integer value"
+"Not a valid float value"
+"Not a valid datetime value"
+"Not a valid date value"
+"Invalid Choice: could not coerce"
+"Not a valid choice"
+"Invalid choice(s): one or more data inputs could not be coerced"
+"'%(value)s' is not a valid choice for this field"
+"Invalid field name '%s'."
+"Field must be equal to %(other_name)s."
+"Field must be at least %(min)d character long."
+"Field cannot be longer than %(max)d character."
+"Field must be between %(min)d and %(max)d characters long."
+"Number must be greater than %(min)s."
+"Number must be less than %(max)s.", 小于%(max)s的数字
+"Number must be between %(min)s and %(max)s."
+"This field is required.", 该项为必填项
+"Invalid input."
+"Invalid email address."
+"Invalid IP address."
+"Invalid URL."
+"Invalid value, must be one of: %(values)s."
+"Invalid value, can't be any of: %(values)s."
+
+
+"Username", 用户名
+"Username can only contain English characters and digits"
+"Email", 邮箱
+"Password", 密码
+"Confirm", 确认密码
+"Username can only contain English characters and digits", 用户名必须为英文字符或数字
@@ -0,0 +1,52 @@
+<!DOCTYPE HTML>
+<html lang="en">
+ <head>
+ <meta charset="UTF-8">
+ <title>Signup</title>
+ <style type="text/css">
+ .fm{padding:14px;}
+ .fm ul{list-style-type:none;margin:0;padding:0;}
+ .fm-row{padding:5px 1px 7px 130px;line-height:24px;zoom:1;}
+ .fm-row:after{display:block;content:"";visibility:hidden;font-size:0;clear:both;height:0;}
+ .fm-item{padding-bottom:3px;}
+ .fm-label{float:left;width:120px;margin-top:5px;margin-left:-130px;color:#222;background:none;text-align:right;vertical-align:middle;}
+ .fm-required .fm-label{font-weight:bold;}
+ .fm-required .fm-label:after{content:"*";padding-left:2px;vertical-align:middle;color:#cc3300;}
+ .fm-item input.fm-text{width:300px;}
+ .fm-item input,.fm-text{float:left;border:1px solid #dadada;padding:6px;outline:0 none;font-size:16px;color:#333;border-radius:4px;text-shadow:0 1px 0 #fff;}
+ .fm-item input:focus,.fm-text:focus,#recaptcha_response_field:focus{border-color:#c1c1c1;}
+ .fm-item select,select.fm-text{padding:0 6px;margin-top:9px;background:#fff;}
+ .fm-item textarea{width:360px;height:3em;}
+ .fm-item .fm-radio{float:left;margin-right:14px;}
+ .fm-radio input{margin:3px;}
+ .fm-explain{font-size:0.7em;color:#999;clear:left;}
+ .fm-error,.fm-error li{margin:0;color:#D14747;}
+ .ui-btn{cursor:pointer;display:inline-block;background-color:#e6e6e6;background-repeat:no-repeat;background-image:-webkit-linear-gradient(270deg, #ffffff 0, #e6e6e6 100%);background-image:-moz-linear-gradient(270deg, #ffffff 0, #e6e6e6 100%);background-image:-ms-linear-gradient(270deg, #ffffff 0, #e6e6e6 100%);background-image:-o-linear-gradient(270deg, #ffffff 0, #e6e6e6 100%);background-image:linear-gradient(270deg, #ffffff 0, #e6e6e6 100%);filter:progid:dximagetransform.microsoft.gradient(startColorstr='#ffffff', endColorstr='#e6e6e6', GradientType=0);padding:5px 24px 6px;text-shadow:0 1px 1px rgba(255, 255, 255, 0.75);color:#333;font-weight:bold;font-family:inherit;line-height:normal;border:1px solid #dadada;border-bottom-color:#bbb;border-radius:4px;box-shadow:inset 0 1px 0 rgba(255, 255, 255, 0.2),0 1px 2px rgba(0, 0, 0, 0.05);-webkit-transition:0.1s linear all;-moz-transition:0.1s linear all;-ms-transition:0.1s linear all;-o-transition:0.1s linear all;transition:0.1s linear all;}
+ .ui-btn:hover{background-position:0 -15px;color:#333333;text-decoration:none;cursor:pointer;}
+ .ui-btn{font-size:1em;font-weight:bold;}
+ </style>
+ </head>
+ <body>
+ <form action="/" method="post" class="fm">
+ {% for field in form %}
+ <div class="fm-row">
+ <div class="fm-item{%if field.required%} fm-required{%end%}">
+ {{field.label()}} {{field(class_='fm-text')}}
+ </div>
+ {% if field.errors or field.description %}
+ <div class="fm-explain">
+ {% if field.errors %}
+ <ul class="fm-error">{% for error in field.errors %}<li>{{error}}</li>{% end %}</ul>
+ {% else %}
+ {{ field.description }}
+ {% end %}
+ </div>
+ {% end %}
+ </div>
+ {% end %}
+ <div class="fm-row">
+ <button type="submit" class="ui-btn">{{ _("Signup") }}</button>
+ </div>
+ </form>
+ </body>
+</html>

0 comments on commit 62e263b

Please sign in to comment.