Rumah >pembangunan bahagian belakang >Tutorial Python >Flask框架返回GET请求的参数为什么会导致500 Internal Server Error?
<code class="language-python3"><span class="k">def</span> <span class="nf">make_response</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">rv</span><span class="p">):</span>
<span class="o">...</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">rv</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_class</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">rv</span><span class="p">,</span> <span class="p">(</span><span class="n">text_type</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">,</span> <span class="nb">bytearray</span><span class="p">)):</span>
<span class="n">rv</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_class</span><span class="p">(</span><span class="n">rv</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="n">status</span><span class="p">)</span>
<span class="n">headers</span> <span class="o">=</span> <span class="n">status</span> <span class="o">=</span> <span class="k">None</span>
<span class="c"># rv是 method handle返回的结果,在k神代码中是 <type></type></span>
<span class="k">else</span><span class="p">:</span>
<span class="n">rv</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_class</span><span class="o">.</span><span class="n">force_type</span><span class="p">(</span><span class="n">rv</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">environ</span><span class="p">)</span>
</code>
因为type(data)返回的是一个type类型,而函数不允许return一个type类型。改成这样:<code class="language-python"><span class="k">def</span> <span class="nf">safestr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s">'utf-8'</span><span class="p">):</span>
<span class="sd">r"""</span>
<span class="sd"> Converts any given object to utf-8 encoded string. </span>
<span class="sd"> </span>
<span class="sd"> >>> safestr('hello')</span>
<span class="sd"> 'hello'</span>
<span class="sd"> >>> safestr(u'\u1234')</span>
<span class="sd"> '\xe1\x88\xb4'</span>
<span class="sd"> >>> safestr(2)</span>
<span class="sd"> '2'</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">unicode</span><span class="p">):</span>
<span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">encoding</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
<span class="k">return</span> <span class="n">obj</span>
<span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s">'next'</span><span class="p">):</span> <span class="c"># iterator</span>
<span class="k">return</span> <span class="n">itertools</span><span class="o">.</span><span class="n">imap</span><span class="p">(</span><span class="n">safestr</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
</code>
我写的flask应用中,methods是写在app.route装饰器里面的。