-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
231 lines (184 loc) · 41.8 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>MarkDown</title>
<subtitle>「靡不有初,鲜克有终」</subtitle>
<link href="/atom.xml" rel="self"/>
<link href="http://blog.tunpok.com/"/>
<updated>2016-05-12T13:12:30.000Z</updated>
<id>http://blog.tunpok.com/</id>
<author>
<name>Ching</name>
</author>
<generator uri="http://hexo.io/">Hexo</generator>
<entry>
<title>TastyPie Note 1</title>
<link href="http://blog.tunpok.com/2016/05/10/TastyPie-Note-1/"/>
<id>http://blog.tunpok.com/2016/05/10/TastyPie-Note-1/</id>
<published>2016-05-10T09:00:00.000Z</published>
<updated>2016-05-12T13:12:30.000Z</updated>
<content type="html"><h3 id="Flow_Through_The_Request/Response_Cycle"><a href="#Flow_Through_The_Request/Response_Cycle" class="headerlink" title="Flow Through The Request/Response Cycle"></a>Flow Through The Request/Response Cycle</h3><p>Tastypie can be thought of as a set of class-based view that provide the API functionality. All routing/middleware/response-handling aspectss are the same as a typical Django app. Where the differs is in the view itself.</p>
<p>Walking through what a GET request to a list endpoint looks like:</p>
<ul>
<li><p>The <code>Resource.urls</code> are checked by Django’s url resolvers.</p>
</li>
<li><p>On a match for the list view, <code>Resource.wrap_view(&#39;dispatch_list&#39;)</code> is called. <code>wrap_view</code> provides basic error handling &amp; allows for returning serilized errors.</p>
</li>
<li><p>Because dispatch_list was passed to <code>wrap_view</code>, <code>Resource.dispatch_list</code> is called next. This is a thin wrapper around <code>Resource.dispatch</code>.</p>
</li>
<li><p><code>dispatch</code> does a bunch of havy lifting. It ensures:</p>
<ul>
<li>the requested HTTP method is in <code>allowed_methos</code> (<code>method_check</code>).</li>
<li>the class has a method that can handle the request(<code>get_list</code>)</li>
<li>the user is authenticated(<code>is_authenticated</code>)</li>
<li>the user has no exceeded their throttle(<code>throttle_check</code>).</li>
</ul>
<p>At this point, <code>dispatch</code> actually calls the requested method (<code>get_list</code>).</p>
</li>
<li><p><code>get_list</code> does the actual work of API. It does:</p>
<ul>
<li>A fetch of the available objects via <code>Resource.obj_get_list</code>. In the case of <code>ModelResource</code>, this builds the ORM filters to apply (<code>ModelResource.build_filters</code>). It then gets the <code>QuerySet</code> via <code>ModelResource.get_object_list</code> (which performs <code>Resource.authorized_read_list</code> to possibly limit the set the user can work with) and applies the built filters to it.</li>
<li>It then sorts the objects based on user input (<code>ModelResource.apply_sorting</code>).</li>
<li>Then it paginates the results using the supplied <code>Paginator</code> &amp; pulls out the data to be serialized.</li>
<li>The objects in the page have <code>full_dehydrate</code> applied to each of them, causing Tastypie to traslate the raw object data into the fields the endpoint supports.</li>
<li>Finally, it calls <code>Resource.create_response</code>.</li>
</ul>
</li>
<li><p><code>create_response</code> is a shortcut method that:</p>
<ul>
<li>Determines the desired response format (<code>Resource.determine_format</code>).</li>
<li>Serializes the data given to it in the proper format.</li>
<li>Returns a Django <code>HttpResponse</code> (200 OK) with the serialized data.</li>
</ul>
</li>
<li><p>We bubble back up the call stack to <code>dispatch</code>. The last thing <code>dispatch</code> does is potentially store that a request occured for future throttling (<code>Resource.log_throttled_access</code>) then either returns the <code>HttpResponse</code> or wraps whatever data came back in a response (so Django doesn’t freak out).</p>
</li>
</ul>
</content>
<summary type="html">
<h3 id="Flow_Through_The_Request/Response_Cycle"><a href="#Flow_Through_The_Request/Response_Cycle" class="headerlink" title="Flow Through T
</summary>
</entry>
<entry>
<title>Tastypie</title>
<link href="http://blog.tunpok.com/2016/05/04/Tastypie/"/>
<id>http://blog.tunpok.com/2016/05/04/Tastypie/</id>
<published>2016-05-04T04:02:00.000Z</published>
<updated>2016-05-05T07:08:52.000Z</updated>
<content type="html"><h4 id="Resources_in_Tastypie"><a href="#Resources_in_Tastypie" class="headerlink" title="Resources in Tastypie"></a>Resources in Tastypie</h4><p>Resources are the heart of Tastypie. By defining a resource we can actually convert a model into an API stream. The data is automatically converted into API response.</p>
<p>Understanding the process of creating a resource.</p>
<ol>
<li>Import ModelResource from Tastypie.</li>
<li>Import models from services app</li>
<li>Create custom resource by inheriting ModelResource and link app model in inner Meta class of resource. </li>
</ol>
<p>Add API URL in the urls.py of app.</p>
<h4 id="Dehydrating_the_JSON_data"><a href="#Dehydrating_the_JSON_data" class="headerlink" title="Dehydrating the JSON data"></a>Dehydrating the JSON data</h4><p><img src="https://impythonist.files.wordpress.com/2016/04/tastypie_ill.png?w=800" alt="flow"></p>
<p>Dehydration in Tastypie means making alterations before sending data to the client. Suppose we need to send capitalized product names instead of small letters. Now we see two kinds of dehydrate methods.</p>
<h5 id="Dehydrate_field_method"><a href="#Dehydrate_field_method" class="headerlink" title="Dehydrate_field method"></a>Dehydrate_field method</h5><p>This <code>dehydrate_field</code> is uesd to modify field on the response JSON. </p>
<h5 id="Dehydrate_method"><a href="#Dehydrate_method" class="headerlink" title="Dehydrate method"></a>Dehydrate method</h5><p>Dehydrate method is useful for aadding additional fields to bundle (response data). </p>
<p>Similarly using <code>hydrate</code> method we can alter the bundle data which is generated from request at the time of PUT or POST methods.</p>
</content>
<summary type="html">
<h4 id="Resources_in_Tastypie"><a href="#Resources_in_Tastypie" class="headerlink" title="Resources in Tastypie"></a>Resources in Tastypie</
</summary>
</entry>
<entry>
<title>Django Manager Method</title>
<link href="http://blog.tunpok.com/2016/04/25/Django-Manager-Method/"/>
<id>http://blog.tunpok.com/2016/04/25/Django-Manager-Method/</id>
<published>2016-04-24T16:00:01.000Z</published>
<updated>2016-04-24T16:20:08.000Z</updated>
<content type="html"><h4 id="Django_Manager"><a href="#Django_Manager" class="headerlink" title="Django Manager"></a>Django Manager</h4><p>Django 里会为每一个 model 生成一个 Manager,默认名字为 objects,一般情况下对 model 进行的处理都是通过 model.objects.XXX( ) 来进行的。其实是调用了 model 的 manager 的方法,而 manager 之中的方法是 QuerySet 方法的代理,QuerySet 方法是对数据库操作的封装。</p>
<p>eg.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> django.db <span class="keyword">import</span> models</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span><span class="params">(models.Model)</span>:</span></span><br><span class="line"> ...</span><br><span class="line"> people = models.Manager()</span><br></pre></td></tr></table></figure>
<p>上面这个 model,<code>Person.objects</code>会产生一个<code>AttributeError</code>,但是<code>Person.people</code>就可以正常操作。因为默认的 manager 已经变成 people,objects 这个 manager 没有重新声明,不起作用。</p>
<h4 id="u81EA_u5B9A_u4E49_Manager"><a href="#u81EA_u5B9A_u4E49_Manager" class="headerlink" title="自定义 Manager"></a>自定义 Manager</h4><p>通常需要自定义 manager 的情况有两点:</p>
<ol>
<li>需要修改/扩展 Django 的 manager 方法</li>
<li>需要修改返回的 QuerySet</li>
</ol>
<h4 id="u9ED8_u8BA4_Manager"><a href="#u9ED8_u8BA4_Manager" class="headerlink" title="默认 Manager"></a>默认 Manager</h4><p>如果使用自定义的 manager 需要注意的是,Django 将 model 中定义的第一个 manager 认为是默认 manager,而且 Django 框架中会用到默认 manager。</p>
<p>笨方法是使用自定义 manager 的时候,对于 model 依然提供 objects 这个默认 manager,并放在第一个。</p>
<p>eg.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Book</span><span class="params">(models.Model)</span>:</span></span><br><span class="line"> title = models.CharField(max_length=<span class="number">100</span>)</span><br><span class="line"> author = models.CharField(max_length=<span class="number">50</span>)</span><br><span class="line"> </span><br><span class="line"> objects = models.Manager() <span class="comment"># default manager</span></span><br><span class="line"> custom_objects = CustomBOokManager() <span class="comment"># custom manager</span></span><br></pre></td></tr></table></figure>
<p><a href="http://blog.csdn.net/sicofield/article/details/49283751" target="_blank" rel="external">source</a></p>
</content>
<summary type="html">
<h4 id="Django_Manager"><a href="#Django_Manager" class="headerlink" title="Django Manager"></a>Django Manager</h4><p>Django 里会为每一个 model 生成
</summary>
</entry>
<entry>
<title>Flask Day 2</title>
<link href="http://blog.tunpok.com/2016/02/16/Flask-Day-2/"/>
<id>http://blog.tunpok.com/2016/02/16/Flask-Day-2/</id>
<published>2016-02-16T14:45:06.000Z</published>
<updated>2016-04-24T16:18:50.000Z</updated>
<content type="html"><p>To handle web forms we use <a href="http://packages.python.org/Flask-WTF" target="_blank" rel="external">Flask-WTF </a>. So we need to write a config file (file <code>config.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">WTF_CSRF_ENABLED = <span class="keyword">True</span></span><br><span class="line">SECRET_KEY = <span class="string">'you-will-never-guess'</span></span><br></pre></td></tr></table></figure>
<p>And then you need to use this config (file <code>app/__init__.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line">app.config.from_object(<span class="string">'config'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>Let’s build a simple form (file <code>app/forms.app</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask.ext.wtf <span class="keyword">import</span> Form</span><br><span class="line"><span class="keyword">from</span> wtforms <span class="keyword">import</span> StringField, BooleanField</span><br><span class="line"><span class="keyword">from</span> wtforms.validators <span class="keyword">import</span> DataRequired</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LoginForm</span><span class="params">(Form)</span>:</span></span><br><span class="line"> openid = StringField(<span class="string">'openid'</span>, validators=[DataRequired()])</span><br><span class="line"> remember_me = BooleanField(<span class="string">'remember_me'</span>, default=<span class="keyword">False</span>)</span><br></pre></td></tr></table></figure>
<p>The <code>DataRequired()</code> is a validator that checks the field is empty or not.</p>
<p>After that, we need a HTML page to show the form (file <code>app/templates/login.html</code>):</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- extend from base layout --&gt;</span></span><br><span class="line">&#123;% extends "base.html" %&#125;</span><br><span class="line"></span><br><span class="line">&#123;% block content %&#125;</span><br><span class="line"> <span class="tag">&lt;<span class="title">h1</span>&gt;</span>Sign In<span class="tag">&lt;/<span class="title">h1</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="title">form</span> <span class="attribute">action</span>=<span class="value">""</span> <span class="attribute">method</span>=<span class="value">"post"</span> <span class="attribute">name</span>=<span class="value">"login"</span>&gt;</span></span><br><span class="line"> &#123;&#123; form.hidden_tag() &#125;&#125;</span><br><span class="line"> <span class="tag">&lt;<span class="title">p</span>&gt;</span></span><br><span class="line"> Please enter your OpenID:<span class="tag">&lt;<span class="title">br</span>&gt;</span></span><br><span class="line"> &#123;&#123; form.openid(size=80) &#125;&#125;<span class="tag">&lt;<span class="title">br</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="title">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="title">p</span>&gt;</span>&#123;&#123; form.remember_me &#125;&#125; Remember Me<span class="tag">&lt;/<span class="title">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="title">p</span>&gt;</span><span class="tag">&lt;<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"submit"</span> <span class="attribute">value</span>=<span class="value">"Sign In"</span>&gt;</span><span class="tag">&lt;/<span class="title">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="title">form</span>&gt;</span></span><br><span class="line">&#123;% endblock %&#125;</span><br></pre></td></tr></table></figure>
<p>The final step is to code a view function that renders the template and receiving data from form (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template, flash, redirect</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line"><span class="keyword">from</span> .forms <span class="keyword">import</span> LoginForm</span><br><span class="line"></span><br><span class="line"><span class="comment"># index view function suppressed for brevity</span></span><br><span class="line"></span><br><span class="line">app.route(<span class="string">'/login'</span>, methods=[<span class="string">'GET'</span>, <span class="string">'POST'</span>])</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">login</span><span class="params">()</span>:</span></span><br><span class="line"> form = LoginForm()</span><br><span class="line"> <span class="keyword">if</span> form.validate_on_submit():</span><br><span class="line"> flash(<span class="string">'Login requested for OpenID="%s", remember_me=%s'</span> %</span><br><span class="line"> (form.openid.data, str(form.remember_me.data)))</span><br><span class="line"> <span class="keyword">return</span> redirect(<span class="string">'/index'</span>)</span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'login.html'</span>, </span><br><span class="line"> title=<span class="string">'Sign In'</span>,</span><br><span class="line"> form=form)</span><br></pre></td></tr></table></figure>
</content>
<summary type="html">
<p>To handle web forms we use <a href="http://packages.python.org/Flask-WTF" target="_blank" rel="external">Flask-WTF </a>. So we need to
</summary>
</entry>
<entry>
<title>Flask Day 1</title>
<link href="http://blog.tunpok.com/2016/02/15/Flask-Day-1/"/>
<id>http://blog.tunpok.com/2016/02/15/Flask-Day-1/</id>
<published>2016-02-14T17:23:33.000Z</published>
<updated>2016-04-24T16:18:08.000Z</updated>
<content type="html"><h3 id="u201CHello_World_u201D_in_Flask"><a href="#u201CHello_World_u201D_in_Flask" class="headerlink" title="“Hello World” in Flask"></a>“Hello World” in Flask</h3><p>Create a folder named <code>microblog</code> (or whatever you want). Then cd into that folder and run following prompt in terminal:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ python3 -m venv flask</span><br></pre></td></tr></table></figure>
<p>Now you’ll have a folder named <code>flask</code> inside <code>microblog</code>, containing a private version of Python interpreter.</p>
<p>And you should install <strong>flask</strong> and extensions by the commands below:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">$ flask/bin/pip install flask</span><br><span class="line">$ flask/bin/pip install flask-login</span><br><span class="line">$ flask/bin/pip install flask-openid</span><br><span class="line">$ flask/bin/pip install flask-mail</span><br><span class="line">$ flask/bin/pip install flask-sqlalchemy</span><br><span class="line">$ flask/bin/pip install sqlalchemy-migrate</span><br><span class="line">$ flask/bin/pip install flask-whooshalchemy</span><br><span class="line">$ flask/bin/pip install flask-wtf</span><br><span class="line">$ flask/bin/pip install flask-babel</span><br><span class="line">$ flask/bin/pip install guess_language</span><br><span class="line">$ flask/bin/pip install flipflop</span><br><span class="line">$ flask/bin/pip install coverage</span><br></pre></td></tr></table></figure>
<p>After that, let’s create the basic structure for our application: <code>app</code> <code>app/static</code> <code>app/templates</code> <code>tmp</code>.</p>
<ol>
<li><code>app</code> — where the application package is</li>
<li><code>static</code> — stores static files like images, javascripts, and css.</li>
<li><code>templates</code> — where templates will go.</li>
</ol>
<p>Then you can start with <code>__init__.py</code> which should put into app folder (file <code>app/__init__.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>The views are the handlers that response to requests from web browsers or other clients. Each view function is mapped to one or more request URLs.</p>
<p>Let’s see what a views function looks like (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>Finally we should create a script to starts up the web server with our application(file <code>run.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!flask/bin/python</span></span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line">app.run(debug=<span class="keyword">True</span>)</span><br></pre></td></tr></table></figure>
<p>To indicating that is an executable file you need to run this in terminal:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ chmod a+x run.py</span><br></pre></td></tr></table></figure>
<p>Now the file structure should look like:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">microblog\&#10; flask\&#10; &#60;virtual environment files&#62;&#10; app\&#10; static\&#10; templates\&#10; __init__.py&#10; views.py&#10; tmp\&#10; run.py</span><br></pre></td></tr></table></figure>
<p>Then start to write the template (file <code>app/templates/index.html</code>):</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="title">html</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"> <span class="tag">&lt;<span class="title">head</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="title">title</span>&gt;</span>&#123;&#123; title &#125;&#125; - microblog<span class="tag">&lt;/<span class="title">title</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="title">head</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"> <span class="tag">&lt;<span class="title">body</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="title">h1</span>&gt;</span>Hello, &#123;&#123; user.nickname &#125;&#125;!<span class="tag">&lt;/<span class="title">h1</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="title">body</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="title">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>Now let’s write the view function that uses this template (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line"></span><br><span class="line"><span class="decorator">@app.route('/')</span></span><br><span class="line"><span class="decorator">@app.route('/index')</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></span><br><span class="line"> user = &#123;<span class="string">'nickname'</span>: <span class="string">'ching'</span>&#125; <span class="comment"># fake user</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>,</span><br><span class="line"> title=<span class="string">'Home'</span>,</span><br><span class="line"> user=user)</span><br></pre></td></tr></table></figure>
<p><code>render_template</code> function is what we import from Flask framework to render the template. It uses <a href="http://jinja.pocoo.org/" target="_blank" rel="external">Jinja2</a> templating engine.</p>
</content>
<summary type="html">
<h3 id="u201CHello_World_u201D_in_Flask"><a href="#u201CHello_World_u201D_in_Flask" class="headerlink" title="“Hello World” in Flask"></a>“H
</summary>
</entry>
<entry>
<title>First Post</title>
<link href="http://blog.tunpok.com/2016/02/12/first-post/"/>
<id>http://blog.tunpok.com/2016/02/12/first-post/</id>
<published>2016-02-12T10:00:00.000Z</published>
<updated>2016-04-24T16:17:24.000Z</updated>
<content type="html"><p>This is the very first post I wrote,</p>
<p>with <a href="https://www.typora.io/" target="_blank" rel="external">Typora</a> &amp; <a href="https://hexo.io/" target="_blank" rel="external">Hexo</a>.</p>
</content>
<summary type="html">
<p>This is the very first post I wrote,</p>
<p>with <a href="https://www.typora.io/" target="_blank" rel="external">Typora</a> &amp; <a href
</summary>
</entry>
</feed>