简体   繁体   English

Python Django:使用Google App Engine处理网址-发布然后获取

[英]Python Django: Handling URL with Google App Engine - Post then Get

I have something like this set up: 我有这样的设置:

class CategoryPage (webapp.RequestHandler):
def get(self):
    ** DO SOMETHING HERE **
def post(self):
    ** DO SOMETHING HERE **
    ** RENDER THE SAME AS get(self)

The question is, after I process the posted data, how would I be able to display the same information as the get(self) function? 问题是,在处理发布的数据之后,如何显示与get(self)函数相同的信息?

A redirect, as others suggest, does have some advantage, but it's something of a "heavy" approach. 正如其他人建议的那样,重定向确实具有一些优势,但这是一种“繁重的”方法。 As an alternative, consider refactoring the rendering part into a separate auxiliary method def _Render(self): and just ending both the get and post methods with a call to self.Render() . 作为一种替代方法,考虑将渲染部分重构为单独的辅助方法def _Render(self):并仅通过调用self.Render()结束getpost方法。

Call self.redirect(url) to redirect the user back to the same page over GET. 调用self.redirect(url)以通过GET将用户重定向回到同一页面。 That way, they won't accidentally re-submit the form if they hit refresh. 这样,如果他们点击刷新,他们就不会意外地重新提交表格。

create_object(request, form_class=FormClass,
        post_save_redirect=reverse('-get-url-handler-',
                                   kwargs=dict(key='%(key)s')))

I use the above django shortcut from generic views where you can specify post save redirect , get in your case . 我从通用视图使用上面的django快捷方式,您可以在其中指定发布后重定向,以您的情况为准。 There are few more examples in this snippet . 在此代码段中,几乎没有其他示例。 Btw, I assumed that you are using django ( helper or patch) with app engine , based on the title of the question. 顺便说一句,我假设您根据问题的标题将django(帮助程序或补丁)与App Engine一起使用。 If you are using app engine patch , check out the views.py in "myapp" app sample add_person handler does what you are looking for. 如果您使用的是App Engine补丁程序,请在“ myapp”应用程序示例add_person处理程序中查看views.py进行所需的操作。

That's generally not a good idea as it'll cause confusion. 通常这不是一个好主意,因为它会引起混乱。 You should really do whatever it is you want to do and then redirect them to the get method. 您实际上应该做任何您想做的事,然后将它们重定向到get方法。

Actually, your code isn't Django, but webapp (Google's mini-"framework"). 实际上,您的代码不是Django,而是webapp(Google的微型“框架”)。 Please read the Django documentation: http://docs.djangoproject.com/ 请阅读Django文档: http : //docs.djangoproject.com/

Django's generic views are only available with app-engine-patch. Django的通用视图仅适用于app-engine-patch。 The helper doesn't support them. 助手不支持他们。 You could take a look at the app-engine-patch sample project to learn more about Django on App Engine: http://code.google.com/p/app-engine-patch/ 您可以看一下app-engine-patch示例项目,以了解有关App Engine上Django的更多信息: http : //code.google.com/p/app-engine-patch/

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

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