Keith Yang
隨隨便便就一打
每個人都可以寫
/用自己喜歡的框架
python get-pip.py
pip install virtualenv
virtualenv taipy
建立 taipy 的 virtualenv 環境. taipy/bin/activate
taipy\scripts\activate
pip install
Django Flask Flask-WTF Flask-SQLAlchemy
Django 1.6.5
(taipy)% django-admin.py startproject mysite
(taipy)% tree mysite/
mysite/
- manage.py
- mysite/
- __init__.py
- settings.py
- urls.py
- wsgi.py
1 directory, 5 files
Flask 0.10.1
(taipy)% mkdir myleaf
(taipy)% cat > myleaf/leaf.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello Leaf!"
if __name__ == "__main__":
app.run()
Flask
(taipy)% cd myleaf
(taipy)% python leaf.py
* Running on http://127.0.0.1:5000/
Django
(taipy)% cd ../mysite
(taipy)% python manage.py runserver
...
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
- myleaf/
- templates/
- static/
- main/
- __init__.py
- errors.py
- forms.py
- views.py
- __init__.py
- tests/
- __init__.py
- test*.py
- email.py
- models.py
- requirements.txt
- config.py
- myleaf/
- app/
- templates/
- static/
- main/...
- __init__.py
- email.py
- models.py
- migrations/...
- tests/...
- venv/...
- manage.py
- requirements.txt
- config.py
(taipy)% python manage.py startapp excuse
(taipy)% tree mysite/
mysite/
- excuse/
- __init__.py
- admin.py
- models.py
- tests.py
- views.py
- manage.py
- mysite/...
3 directory, 13 files
Application Factoryfrom flask import current_app, Blueprint, render_template
admin = Blueprint('admin', __name__, url_prefix='/admin')
@admin.route('/')
def index():
return render_template(current_app.config['INDEX_TEMPLATE'])
如剛剛的範例,是個從無到有的過程。Flask 沒有限定專案裡的檔案要如何組織。對於剛學習 Flask 的人來說,不需要先知道 Application Factories (
當然,對沒有經驗的初學者,可能會造成不知如何組織一個 Flask 專案的檔案架構的問題,而那依然可由文件與範例來解。
mysite/settings.py
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
...
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'excuse' # <- Newly added Django App
)
mysite/urls.py
urlpatterns = patterns('',
...
url(r'^$', 'excuse.views.home'),
)
mysite/excuse/views.py
from django.http import HttpResponse
def home(request):
excuses = [
"It was working in my head",
"I thought I fixed that",
"Actually, that is a feature",
"It works on my machine",
]
return HttpResponse(excuses[0])
request
@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return 'User %s' % username
@app.route('/post/<int:post_id>')
def show_post(post_id):
# show the post with the given (int) id
return 'Post %d' % post_id
'/post/<int:post_id>'
post_id
(int 整數)from flask.views import View
class ShowUsers(View):
def dispatch_request(self):
users = User.query.all()
return render_template('users.html', objects=users)
app.add_url_rule('/users/',
view_func=ShowUsers.as_view('show_users'))
mysite/excuse/templates/index.html
<!DOCTYPE HTML>
<html>
<head>
<title>Clone of Excuses For Lazy Coders</title>
</head>
<body>
<h1> <a href="/" rel="nofollow">{{ excuse }}</a> </h1>
</body>
</html>
myleaf/templates/index.html
<!doctype html>
<title>Hello from Flask</title>
{% if name %}
<h1>Hello {{ name }}!</h1>
{% else %}
<h1>Hello World!</h1>
{% endif %}
mysite/excuse/templates/index.html
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(
label='Your name',
max_length=100)
myleaf/templates/index.html
from flask_wtf import Form
from wtforms import TextField
from wtforms.validators import DataRequired
class MyForm(Form):
name = TextField('name', validators=[DataRequired()])
mysite/excuse/models.py
from django.db import models
class Excuse(models.Model):
content = models.TextField()
def __unicode__(self):
return self.content
>>> from excuse.models import Excuse
>>> Excuse.objects.create(
content="It was working in my head")
>>> Excuse.objects.all()
>>> for excuse in Excuse.objects.all():
>>> print excuse.content
django.contrib.auth
Thanks!
Questions & Answers