Dojango - 道场 + Django,如何“包装”用于 AJAX 的 django.contrib.auth 和 django-registration,单页面使用
我是 Dojo 和 Django 的新手。也就是说,我正在尝试编写一个单页应用程序,但我不明白如何利用 Django 的内置身份验证工具,因为它们是围绕传统的 Django 每页视图模型设计的。
我希望在 @json_response
来显示;但是,我不知道如何“包装”现有视图,以便它们不期望页面加载。
单页 Django 应用程序有任何常规策略吗?我喜欢Django的ORM和Dojo的UI,但它们似乎很难完全集成。谢谢。
##############################################################
# # # #
# LOGOUT # # DISP #
# ########################################## #
# REGSTR # # DISP #
# # MAINPANE # #
# DO_IT # # DISP #
# # Forms, views, etc. # #
# CNTRL # using dojo.xhrGET, xhrPUT # DISP #
# # # #
# QUIT # # DISP #
# # # #
# ########################################## DISP #
# # # #
# # STATUS: MESSAGE # #
# # # #
##############################################################
编辑:为了更明确,我想要这样的流程:
- 用户单击“DO_IT”按钮。
- Dojo xhrGET 获取 DO_IT 表单并用它替换 MAINPANE 的内容。
- 用户使用 DO_IT 表单执行某些操作,并且 dojo xhrPOST 用户的操作。
- Dojo 用响应替换 MAINPANE 的内容。
- 利润
实现这一目标的最佳/传统/常见/记录最多的方法是什么?我知道可能有很多可能的方法。我正在寻找一些作为新手不太容易搞定的东西。
I'm new to Dojo and Django. That said, I'm trying to write a single-page app and I do not understand how to leverage Django's built-in authentication tools since they are designed around the traditional Django page-per view model.
I'd like to have all forms, both specific to my app and for auth and django-registration displayed in the <div dojoType="dijit.layout.contentPane" id="mainPane"></div>
. I have mastered getting views rendered as Dojango's @json_response
to display; however, I do not know how to "wrap" existing views so that they do not expect page loads.
Is there any conventional strategy for single-page Django apps? I like Django's ORM and Dojo's UI, but they seem to be difficult to fully integrate. Thanks.
##############################################################
# # # #
# LOGOUT # # DISP #
# ########################################## #
# REGSTR # # DISP #
# # MAINPANE # #
# DO_IT # # DISP #
# # Forms, views, etc. # #
# CNTRL # using dojo.xhrGET, xhrPUT # DISP #
# # # #
# QUIT # # DISP #
# # # #
# ########################################## DISP #
# # # #
# # STATUS: MESSAGE # #
# # # #
##############################################################
Edit: Just to be more explicit, I want a flow like this:
- User clicks "DO_IT" button.
- Dojo xhrGETs the DO_IT form and replaces MAINPANE's content with it.
- User does something with DO_IT form and dojo xhrPOSTs the user's action.
- Dojo replaces the content of MAINPANE with the response.
- Profit
What is the best/conventional/common/most-documented way of accomplishing this. I know that there are likely many possible approaches. I'm looking for something that is less easy to f@#$-up as a novice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你已经说得差不多了。 django 视图只能返回 #MAINPANE div 中所需的 HTML 块,然后通过设置 MAINPANE 节点的 .innerHTML 属性来插入它。
用户登录后,所有身份验证都应该自动进行,即使是 Ajax 调用也是如此,因此您不必担心这一点。但您确实必须考虑如果身份验证失败会发生什么 - 您最终可能只是将 403 Permission Denied 消息转储到您的 #MAINPANE div 中。
这种网页设计还有一些其他的微妙之处 - 大多数情况下,除非您在某处提供“永久链接”链接,否则无法为页面添加书签,该链接可以将您带到将所需内容加载到 MAINPANE div 中的页面。当浏览器中的“重新加载”不会重新加载页面的当前视图而是跳回“主页”视图时,会让人感到惊讶。
或者你可以用 FRAME 标签用旧的 skool 方式来做:)
You've pretty much spelt it out. The django view can just return the chunk of HTML that you want in the #MAINPANE div, and you insert it by setting the MAINPANE node's .innerHTML property.
Once the user has logged in all the authentication should happen automagically even to Ajax calls, so you don't have to worry about that. But you do have to consider what happens if the authentication fails - you could end up just dumping a 403 Permission Denied message into your #MAINPANE div.
There's a few other subtleties to this kind of web page design - mostly it becomes impossible to bookmark pages unless you provide a 'permalink' link somewhere that gets you to the page with the required content loaded into the MAINPANE div. And hitting reload in the browser causes surprise when it doesn't reload the current view of the page but jumps back to the 'home' view.
Or you could just do it the old skool way with FRAME tags :)