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

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

问题描述

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



我想要的:
此页面的URL指的是一个视图。该视图调用另外两个视图(在此称为子视图),然后每个子视图呈现其部分并返回结果。主视图然后加入子视图的结果并返回。



理想情况下,我将在页面上进行快速的JavaScript检查 - 如果启用了javascript,表单的提交按钮将处理表单添加的子视图Ajax'd,该页面将以这种方式更新。我想我可以触发一个请求来刷新帖子列表或者其他东西。



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



更新:子视图是我组成的一个术语。我想要的是一个视图,可以由Ajax直接调用返回有意义的东西,或从另一个视图(我称之为主视图)。如果这个主视图调用,主视图如何处理从多个子视图返回数据?



有没有一个简单的方法呢?这是否适合的方式来考虑页面中的多个视图?我应该关心责任分离吗?

解决方案

django中的视图只是任何可调用的,最终返回一个Response对象。在这种观点下,您可以将工作分解为适合您的任何组织。也许你的观点100%代表其他方法。



在您的情况下,您的主视图将调用其他2个数据功能。如果他们也接受Request对象并使用它,这些也可以是视图。他们还需要将响应对象返回为django视图,因为这样就可以将URL指向它们。但是,再有两个其他视图返回给您的响应对象并不是真的有用。您可能想要的只是执行特定任务并返回一些数据结构的其他方法,甚至可能是模板的渲染代码段。然后,您将使用这些数据,或将模板字符串合并在一起,并在主响应中返回。



如果您真的设置使用其他视图返回响应对象,那么你可以做一些像抓住身体的东西,并将它们合并到你自己的响应中:

https://docs.djangoproject.com/en/1.4/ref/request-response/



真的没有什么是与教程大不相同。你只是调用数据的其他方法。如果要使其组织起来,您应该将数据处理逻辑与视图功能分开。您的主视图会将这些数据处理函数称为值。而您的子视图只是简单的视图,也称为这些个人数据函数并将其包装到响应中。



Pseudo:

  def mainView(request):
val = data1()
val2 = data2()
response =#val + va2 +其他东西
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 =#获取数据
返回val

def data2():
val2 =#获取数据
返回val2


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).

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.

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: "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?

解决方案

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.

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.

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.

Pseudo:

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天全站免登陆