代码吹过html模板并直接返回python

时间:2012-09-24 13:03:00

标签: python html

This question started as the question at this link

我认为问题出在模板unexpected.html中(下面进一步说明)。 python代码工作正常,但是当用户提供number然后单击提交按钮时,python代码接受该值,无论采用if子句的哪个分支,结果视图浏览器 unexpected.html和python代码底部的嵌入式html。

为什么浏览器不会仅停留unexpected.html模板?

下面首先是python代码。

class MainPage(BaseHandler):

    def get(self):
    self.render_template('index.html', { })

    def post(self):
    number = self.request.get('number')
    hiddennumber = self.request.get('hiddennumber')
    for i in ["a"]:
        if int(number) < int(hiddennumber):
        reason='< was in that time slot already: '
        trans = Trans(key_name='reason')
        trans.reason=reason
        trans.name=number
        trans.put()
        template_values = {'trans':trans}
        path = os.path.join(TEMPLATE_DIR, 'unexpected.html')
        self.response.out.write(template.render(path, template_values))
        elif int(number) > int(hiddennumber):
        reason='> was in that time slot already: '
        trans = Trans(key_name='reason')
        trans.reason=reason
        trans.name=number
        trans.put()
        template_values = {'trans':trans}
        path = os.path.join(TEMPLATE_DIR, 'unexpected.html')
        self.response.out.write(template.render(path, template_values))
        else:
        pass
    self.response.out.write('''
        <html>
          <body>
            <form method="post">
              <p>Name: <input type="text" name="name" /></p>
              <p>Favorite foods:</p>
              <select name="favorite_foods" multiple size="4">
                <option value="apples">Apples</option>
                <option value="bananas">Bananas</option>
                <option value="carrots">Carrots</option>
                <option value="durians">Durians</option>
              </select>
              <p>Birth year: <input type="text" name="birth_year" /></p>
              <p><input type="submit" /></p>
            </form>
          </body>
        </html>
        ''')

class Unexpected(BaseHandler):

    def get(self):
        trans=Trans.get_by_key_name('reason')
        template_values = {'trans':trans}
        path = os.path.join(TEMPLATE_DIR, 'unexpected.html')
        self.response.out.write(template.render(path, template_values))

    def post(self):
        day=self.request.get('day')
        return webapp2.redirect("/")

app = webapp2.WSGIApplication([
        ('/', MainPage), 
        ('/unexpected', Unexpected)
        ],
        debug=True)

模板unexpected.html是下一个。

{% extends "base.html" %}
{% block content %}


<form action="" method="post" >
<input type="hidden" name="day" value="{{ day }}"/>
This unexpected result occurred: <emph style="font-weight: bold">{{ trans.reason }}</emph>
<br /><br />
<div id="inputdata">
<label>Click the "Ok" button to go back to the previous page so you can edit your entry.
</label>
<input type="submit" value="submit" />
</div>
</form>
<button onclick="window.history.back()">Ok</button>

{% endblock content %}

最后,为了更加完整,我将index.html包括在内。

{% extends "base.html" %}
{% block content %}
<center>
   </h1>
  </center>
<form action="" method="post">
<input type="hidden" name="hiddennumber"  value="10">
<label>Location/Venue name (no spaces and case counts)</label>
<input type="textbox" name="number" size="30" value=""></input><br/>
  <input type="submit" value=submit />
</form>


{% endblock content %}

1 个答案:

答案 0 :(得分:1)

您无处可见我可以看到的执行流程,嵌入式HTML将在测试之后,因此没有理由不执行。当你完成时,最快的解决方案就是return

self.response.out.write(template.render(path, template_values))
return
...

通常,在该结构中也有很多重复的逻辑:应该合并,然后通过使该结构更像是一个具有适当显示逻辑的调度程序,可以进一步重构代码以使其更具可读性功能