Django - 两个视图,一页 [英] Django - two views, one page

查看:23
本文介绍了Django - 两个视图,一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个 Django 页面,它显示了一个项目列表,并允许用户填写一个表单以添加到项目中(我们称之为项目帖子).

Say we have a Django page that shows a list of items and allows the user to fill in a form to add to the items (let's call the items posts).

我想要的:此页面的 URL 引用了一个视图.该视图调用另外两个视图(此处称为子视图"),然后每个子视图呈现其部分并返回结果.然后主视图连接子视图的结果并返回.

What I want: The URL for this page refers to a view. The view calls two other views (called "sub-view" hereon), then each sub-view renders its section and returns the results. The main view then joins the results of the sub-views and returns that.

理想情况下,我会在页面上快速检查 javascript - 如果启用了 javascript,则表单的提交按钮将Ajax"到处理表单添加的子视图,页面将是这样更新.我想我也可以触发一个请求来刷新帖子列表或其他什么.

Ideally, I would have a quick javascript check on the page - if javascript is enabled, the submit button for the form will be "Ajax'd" to the sub-view that deals with form adding, and the page will be updated that way. I suppose I could trigger a request to refresh the list of posts afterwards too or something.

那么如何在主视图中连接两个子视图呢?这可能吗?

So how do I concatenate the two sub-views in the main view? Is this possible?

UPDATE:子视图"是我编造的一个术语.我想要的是一个视图,它可以被 Ajax 直接调用以返回一些有意义的东西,或者从另一个视图(我将其称为主视图").如果被这个主视图"调用,主视图如何处理从多个子视图"返回的数据?

UPDATE: "sub-view" is a term I made up. What I want is a view that can be called either by Ajax directly to return something meaningful, or from another view (which I'll call the "main view"). If called by this "main view", how does the main view handle returning the data from multiple "sub-views"?

有没有简单的方法可以做到这一点?这是考虑页面中多个视图的适当方式吗?我应该关心职责分离吗?

Is there a simple way to do this? Is this an appropriate way to think about multiple views in a page? Should I care about separation of responsibilities?

推荐答案

django 中的视图只是最终返回 Response 对象的任何可调用对象.在该视图中,您可以将工作拆分为适合您的任何组织.也许您的视图 100% 委托给其他方法.

A view in django is just any callable that ultimately returns a Response object. Within that view, you could split the work up into whatever organization suits you. Maybe your view 100% delegates out to other methods.

在您的情况下,您的主视图会调用另外 2 个数据函数.如果它们也接受 Request 对象并使用它,它们也可以是视图.他们还需要返回 Response 对象以被视为 django 视图,因为这是您将 URL 指向它们的方式.但是让其他两个视图返回 Response 对象对您来说真的没有任何好处.您可能想要的只是执行特定任务并返回一些数据结构的其他方法,或者甚至是模板的渲染片段.然后您将使用这些数据,或将模板字符串合并在一起并在您的主响应中返回.

In your case, your main view would call 2 other functions for data. These could also be views if they also accept a Request object as well and make use of it. They also would need to return Response objects to be considered django views since that is how you would point URLs at them. But it doesn't really do you any good to have two other views returning you Response objects. What you probably want is just other methods that do specific tasks and return some data structure, or maybe even a rendered snippet of a template. You would then use these data, or merge the template strings together and return that in your main Response.

如果您真的打算使用其他返回 Response 对象的视图,那么您可以执行一些操作,例如从它们中提取主体,并将它们合并到您自己的响应中:
https://docs.djangoproject.com/en/1.4/ref/request-response/

If you are really set on making use of other views that return Response objects, then you can do something like grabbing the body out of them, and merging them into your own response:
https://docs.djangoproject.com/en/1.4/ref/request-response/

真的,没有什么与教程有太大不同.您只是为数据调用其他方法.如果你想让它有条理,你应该将数据处理逻辑与视图功能分开.您的主视图将为值调用这些数据处理函数.而你的子视图"只是简单的视图,它们也调用这些单独的数据函数并将它们包装成一个响应.

Really, nothing is much different from the tutorials. You are just calling other methods for data. If you want to make it organized you should separate the data processing logic from the view function. Your main view would call these data processing functions for values. And your "sub views" would just be simple views that also call these individual data functions and wrap them into a Response.

伪:

def mainView(request):
    val = data1()
    val2 = data2()
    response = # val + va2 + other stuff
    return response

def subView1(request):
    val = data1()
    response = # val  + stuff
    return response 

def subView2(request):
    val2 = data2()
    response = # val2  + stuff
    return response 

def data1():
    val = # get data
    return val 

def data2():
    val2 = # get data
    return val2

这篇关于Django - 两个视图,一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆