简体   繁体   English

在GAE中实施网址段

[英]Implementing URL slugs in GAE

I'm trying to implement URL slugs in my app (python-based). 我正在尝试在我的应用程序(基于python)中实现URL标记。 I want the slugged URL to be of the format myhost/{post_id}/{post_title} . 我希望输入的网址格式为myhost/{post_id}/{post_title}

When I try to access the page using the above slugged URL format, I get an error and (in Chrome, the error says - unexpected token <). 当我尝试使用上述缓慢的URL格式访问页面时,出现错误,并且(在Chrome中,错误提示-意外令牌<)。 If I delete the /<post_title> from the URL, the page loads correctly. 如果我从URL中删除/<post_title> ,则页面正确加载。 Specifically, I noticed that once I have a 'forward slash' after the <post_id> , I have issues. 具体来说,我注意到,一旦在<post_id>之后有一个“正斜杠”,我就会遇到问题。 Everything works fine without that extra slash (extra directory) 一切正常,没有多余的斜线(额外的目录)

My code is: 我的代码是:

class mainhandler(webapp.RequestHandler):
    def get(self):
        if (self.request.path == '/test'):
            path = os.path.join (os.path.dirname (__file__), 'test.htm')
            self.response.headers ['Content-Type'] = 'text/html'
            self.response.out.write (template.render (path, {}))
        else:                       
            path = os.path.join (os.path.dirname (__file__), 'index.htm')            
            self.response.headers ['Content-Type'] = 'text/html'                 
            self.response.out.write (template.render (path, {}))

    application = webapp.WSGIApplication( [('/.*', mainhandler)],  debug=True) 

Basically, I want to load the index.htm file and on that file, I have JavaScript which is supposed to extract the post-id from the URL and do some stuff with it. 基本上,我想加载index.htm文件,在该文件上,我有JavaScript,该JavaScript应该从URL中提取post-id并对其进行处理。

Anybody know what I'm doing wrong here? 有人知道我在这里做错了吗?

You will need to modify the URL path that you have in your application . 您将需要修改application的URL路径。 If I'm understanding correctly, you'll want to do something like this: 如果我理解正确,那么您将需要执行以下操作:

application = webapp.WSGIApplication(
    [('/(.*)/(.*)', mainhandler),
     ('/.*', mainhandler)],
    debug=True)

And then define your mainhandler as: 然后将您的mainhandler定义为:

class mainhandler(webapp.RequestHandler):
    def get(self, post_id=None, post_title=None):

Although it doesn't seem like you are doing anything with them at the moment, once in your mainhandler you will have those variables available to you. 尽管目前看来您并没有对它们做任何事情,但是一旦在mainhandler您将可以使用这些变量。 The reason your first mapping wasn't working was because it wasn't set up to handle additional / 's and items in the URL. 您的第一个映射无法使用的原因是,该映射未设置为处理URL中的其他/项。

And per our subsequent conversation, the cause of the syntaxError (which is a JavaScript-related error) is because of the missing / in front of include/load.js . 并且在我们随后的对话中, syntaxError (与JavaScript相关的错误)的原因是由于include/load.js前面缺少/ Without that prefix, the path is relative to the current location. 如果没有该前缀,则路径是相对于当前位置的。 In the case of localhost:8080/1234/a-test-case , it becomes /1234/a-test-case/include/load.js , which doesn't map to anything specific in app.yaml and therefore falls through to the .* handler (and not the Javascript handler you have defined), which returns you to the main script and follows the hanlder paths there. localhost:8080/1234/a-test-case /1234/a-test-case/include/load.js localhost:8080/1234/a-test-case ,它变成/1234/a-test-case/include/load.js ,它不会映射到app.yaml任何特定内容,因此会陷入.*处理程序(而不是您定义的Javascript处理程序),它使您返回到主脚本并遵循此处的处理程序路径。 The link to your .js. .js.的链接.js. now returns your index.html instead of your .js file, which causes an error (similar to what can be found here ). 现在返回您的index.html而不是.js文件,这将导致错误(类似于在此处可以找到的错误)。

I would consider using webapp2 which has support for extended routes and Python 2.7. 我会考虑使用支持扩展路由和Python 2.7的webapp2。

WebApp2 Routing WebApp2路由

After some extensive discussion with RocketDonkey, I tried something which worked. 与RocketDonkey进行了广泛讨论之后,我尝试了一些有效的方法。

I changed my script file from 我从更改了脚本文件

<script src="include/load.js" type="text/javascript"></script>

to

<script **src="/include/load.js"** type="text/javascript"></script>

RocketDonkey's explanation for the working solution: when you use a front slash, you are saying 'take this URL path from root'. RocketDonkey对有效解决方案的解释:使用斜杠时,您说的是“从根目录获取此URL路径”。 But without it, it is in terms of the current location 但是没有它,就目前位置而言

This explains why the original code worked when my url was simply localhost:8081/1234 but not in the second scenario where I have a second directory 这解释了为什么当我的URL只是localhost:8081/1234而不是在第二个方案中我有第二个目录的情况下原始代码仍然有效的原因

NB: The solution described here was used in conjunction with the original proposal from rocketDonkey 注意:此处描述的解决方案是与rocketDonkey的原始建议结合使用的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM