简体   繁体   中英

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

I'm a new on Django. One question confused me about the drop down list in 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.

In Model.py I design the fields as:

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.

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.
  • or to fetch the content with ajax requests depending on the value of the drop-down and render it when it loads.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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