简体   繁体   English

如何根据Django下拉列表中的选择获取页面

[英]How to get the page based on the selection in a drop down list in Django

I'm a new on Django. 我是Django的新手。 One question confused me about the drop down list in Django. 一个问题使我对Django中的下拉列表感到困惑。 I'd like to know how to change the content in a page based on the selection in the drop down list in Django. 我想知道如何根据Django下拉列表中的选择更改页面中的内容。

In Model.py I design the fields as: Model.py中,我将字段设计为:

STATUS_IN_CHOICE = (
    ('Pending','Pending'),
    ('Passed','Passed'),
    ('Failed','Failed'),
)
Status = models.CharField(max_length=10,choices=STATUS_IN_CHOICE,
                          default='Pending')

The respected result I want is, if I select "Passed" in the drop down list. 我想要的受尊敬的结果是,如果我在下拉列表中选择“通过”。 All the passed cases show in the page. 所有通过的案例都显示在页面中。 Others are the same. 其他都一样。

I have no idea how to code the template and connect with the field now. 我不知道如何编码模板并立即与该领域联系。 Cause in my opinion, it's not a forms (post) at all. 我认为,这根本不是表格(帖子)。

You have basically two alternatives, and both of involve the use of javascript. 您基本上有两种选择,并且都涉及使用javascript。

You either: 您要么:

  • need to preload all content at the initial page render, if the content is small and simple, and then use javascript on the drop-down to display the appropriate parts of it. 如果内容又小又简单,则需要在初始页面呈现时预加载所有内容,然后在下拉菜单中使用javascript来显示其中的适当部分。
  • or to fetch the content with ajax requests depending on the value of the drop-down and render it when it loads. 或根据下拉菜单的值使用ajax请求获取内容并在加载时进行渲染。

Neither of the above the use of a model if the drop-down is for display filter only, and no storing is required. 如果下拉列表仅用于显示过滤器,并且不需要存储,则以上两种方法都不使用模型。 You could achieve the same with a simple django form or plain HTML. 您可以使用简单的Django表单或纯HTML实现相同的功能。

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

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