简体   繁体   English

我试图通过一个大小 <DIV> 回到谷歌应用程序引擎(Python代码)但似乎无法使其工作

[英]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.

相关问题 我正在尝试在JS中编写一个简单的异步代码,但它似乎不起作用 - I am trying to write a simple asynchronous code in JS, but it doesn't seem to work 我正在尝试使用javascript更改表单元格的属性,并且找到了我的建议,但似乎无法使它们正常工作 - I am trying to change the properties of a table cell with javascript and have found my suggestions but I can't seem to get them to work 我似乎无法在我的图书馆应用程序上使用切换开关 - I can't seem to make toggle switch work on my library app 如果在Javascript中运行语句,我似乎无法做到这一点: - I can't seem to make this if statement work in Javascript :S 我目前正在尝试使用 discord.js 编写一个船舶代码,但我无法让它工作 - I am currently trying to write a ship code with discord.js but i can't get it to work 我似乎无法使我的代码正常工作 - I can't seem to get my code to work 如何在href下方的代码中建立链接? 我似乎无法使其成为可点击的链接 - How can I make the link in the code below an href? I can't seem to make it a clickable link 我正在尝试从 <select>列到顶部,但是我的代码似乎不起作用 - I'm trying to move one element from a <select> list to the top, but my code doesn't seem to work 我似乎无法让我的代码显示我的计算,我是否缺少 output 标签? - I can't seem to get my code to display my calculation, am I missing an output tag? 我有这个 python 代码,我试图将其转换为 javascript 但我似乎无法弄清楚是哪个 if 语句。 使用 - I have this python code that I am trying to turn into javascript but I cant seem to figure out which if statement. to use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM