代码吹过html模板并直接返回到python [英] Code blows through html template and returns to python directly

查看:107
本文介绍了代码吹过html模板并直接返回到python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题始于此连结的问题



我认为问题出现在模板 unexpected.html 中(下文更详细)。 python代码可以正常工作,但是当用户提供一个数字然后点击提交按钮时,python代码会接受该值并且不管<$ c $的哪个分支如果使用子句,浏览器中的结果视图是 unexpected.html 和嵌入的html在Python代码的底部。



为什么浏览器停止时只有 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 [in a]:
if int(number)< int(hiddennumber):
reason ='< '
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 ='> '
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>
< b< ; form method =post>
< p>名称:< input type =textname =name/>< / p>
< p> :< / p>
< select name =favorite_foodsmultiple size =4>
< option value =apples>苹果< / option>
< ;选项值=香蕉>香蕉< / option>
< option value =胡萝卜>胡萝卜< / option>
< option value =durians>榴莲< / option>
< / select>
< p>出生年份:< input type =textname =birth_year/>< / p>
< p>< input type =submit/>< / p>
< / form>
< / body>
< / html>
''')

class意外(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):$ b $ day = self.request.get('day')
return webapp2.redirect(/)

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

接下来是模板 unexpected.html

  {%extendsbase.html%} 
{%block content%}


< form action =method =post>
< input type =hiddenname =dayvalue ={{}}/>
出现此意外结果:< emph style =font-weight:bold> {{trans.reason}}< / emph>
< br />< br />
< div id =inputdata>
< label>点击确定按钮返回上一页,以便编辑条目。
< / label>
< input type =submitvalue =submit/>
< / div>
< / form>
< button onclick =window.history.back()> Ok< / button>

{%endblock content%}

最后,我将 index.html 以获得更大的完整性。

  {%extendsbase.html %} 
{%block content%}
< center>
< / h1>
< / center>
< form action =method =post>
< input type =hiddenname =hiddennumbervalue =10>
< label>位置/场地名称(不包含空格和个案数目)< / label>
< input type =textboxname =numbersize =30value =>< / input>< br />
< input type =submitvalue = submit />
< / form>


{%endblock content%}


解决方案

当你完成后,最快的解决方案就是 return

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

一般来说,在这个结构中也有很多重复的逻辑:应该被整合,然后代码可以被进一步重构,使得该结构更像是一个调度器适当的显示逻辑在不同的功能中。

This question started as the question at this link

I think the problem is in the template unexpected.html (further below). The python code works fine, but when the user supplies a number and then clicks the submit button, the python code accepts the value and no matter which branch of the if clauses are taken, the resulting view in the browser is both the unexpected.html and the embedded html at the bottom of the python code.

Why doesn't the browser stop with just the unexpected.html template?

First below is the python code.

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)

The template unexpected.html is next.

{% 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 %}

Finally, I am including index.html for greater completeness.

{% 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 %}

解决方案

Nowhere do you break out of the flow of execution that I can see, and the embedded HTML would be after the tests so there's no reason it wouldn't execute. The fastest solution would just be to return when you're done:

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

In general you also have a lot of duplicated logic in that structure also: that should be consolidated and then the code could be further refactored to be more readable by making that structure more of just a dispatcher with the appropriate display logic in separate functions.

这篇关于代码吹过html模板并直接返回到python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆