[英]I am trying to pass the size of a <DIV> back to google app engine (Python code) but can't seem to make it work
Many apologies if I'm being really dopey here - I've been searching most of the morning for the answer to this but maybe I'm searching in the wrong terms. 如果我在这里真的很蠢,很多人道歉 - 我早上大部分时间都在寻找答案,但也许我正在用错误的语言搜索。 I'm using Google App Engine, with Python code to set up my webapp. 我正在使用Google App Engine,使用Python代码来设置我的webapp。 I've managed to set up the basic page structure using the following HTML code: 我已设法使用以下HTML代码设置基本页面结构:
main_html = """
<HTML>
<HEAD>
<LINK rel="stylesheet" type="text/css" href="static/css/main.css">
</HEAD>
<BODY>
<DIV class="wspace">
<BR>
</DIV>
<DIV class="header">
<FONT class="logoFont">Logo Text</font>
</DIV>
<DIV class="maincontainer">
<DIV class="sidebar" id="leftsidebar">
<SCRIPT>
var lsCanWid = document.getElementsByTagName("div"["leftsidebar"].offsetWidth
var lsCanHei = document.getElementsByTagName("div")["leftsidebar"].offsetHeight
</SCRIPT>
</DIV>
<DIV class="sidebar" id="rightsidebar">
<SCRIPT>
var rsCanWid = document.getElementsByTagName("div")["rightsidebar"].offsetWidth
var rsCanHei = document.getElementsByTagName("div")["rightsidebar"].offsetHeight
</SCRIPT>
</DIV>
<DIV class="mainscreen" id="mainscr">
<SCRIPT>
var mainCanWid = document.getElementsByTagName("div")["mainscr"].offsetWidth
var mainCanHei = document.getElementsByTagName("div")["mainscr"].offsetHeight
</SCRIPT>
%(MAINCONTENT)s
</DIV>
</DIV>
<DIV class="footer">
Footer Text
</DIV>
<DIV class="wspace">
<FONT class="crnotice">Copyright Notice Text</FONT>
</DIV>
</BODY>
</HTML>
"""
I've tested the javascript variables, and these work exactly as planned (lsCanWid returns the left sidebar width for example). 我已经测试了javascript变量,这些变量完全按计划工作(例如lsCanWid返回左侧边栏宽度)。
I've then got the following code in my Python: 我在Python中得到了以下代码:
class MainHandler(webapp2.RequestHandler):
def get(self):
lsCanWid = self.request.get('lsCanWid')
lsCanHei = self.request.get('lsCanHei')
rsCanWid = self.request.get('rsCanWid')
rsCanHei = self.request.get('rsCanHei')
mainCanWid = self.request.get('mainCanWid')
mainCanHei = self.request.get('mainCanHei')
temp_str = str(lsCanWid)
self.response.write(main_html %{"MAINCONTENT": temp_str})
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
I've not tried to use self.request.get within a "get" before, so I'm not sure if this is the problem. 我以前没试过在“get”中使用self.request.get,所以我不确定这是不是问题。 The python code is working fine if you replace temp_str with some other string, so that's not where my issue lies. 如果你用其他字符串替换temp_str,python代码工作正常,所以这不是我的问题所在。 I'd be really grateful of any help here! 我真的很感激这里的任何帮助!
In short - You aren't sending these values back to your handler, you need to add them to the query string or post them back. 简而言之 - 您不是将这些值发送回处理程序,您需要将它们添加到查询字符串或将其发回。
Javascript runs the browser. Javascript运行浏览器。 Once the page has rendered in the browser, there is no connection with your back end code. 页面在浏览器中呈现后,与后端代码无关。 In order for your Python code to know anything from the browser, you have to send it back to the code. 为了使您的Python代码能够从浏览器中了解任何内容,您必须将其发送回代码。 This is just how HTTP works. 这就是HTTP的工作原理。 Its a "request/response" cycle and there is no connection once the response (what you send to the browser) is finished. 它是一个“请求/响应”周期,一旦响应(发送给浏览器的内容)完成,就没有连接。
So, to send these values back to your code you need to append them to the request as part of a query string, something like /foo?lsCanWid=3&lsCanHei=4
(and so on). 因此,要将这些值发送回代码,您需要将它们作为查询字符串的一部分附加到请求中,例如/foo?lsCanWid=3&lsCanHei=4
(依此类推)。 You would need to build this URL using javascript, and then add it to a link which you would have to click - then, on the next request (once this new link is clicked), your Python code will receive the values. 您需要使用javascript构建此URL,然后将其添加到您必须单击的链接 - 然后, 在下一个请求 (单击此新链接)后,您的Python代码将接收值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.