Search code examples
pythonhtmldjangodropdown

DJango - Update text depending on dropdown selection?


My Google search skills evidently aren't up to scratch, so I'm wondering if Django has the capability to dynamically update text displayed on the HTML page depending on what option is selected from a dropdown?

Or would I need to look into utilising another tool to help with this?

Apologies for the noob question as I'm still learning Python/Django.


Solution

  • This can only be achieved via frontend scripting, so JavaScript. Depending on what you are using in your templates, you might have a possibility to use that library or write javascript yourself.

    I think the easiest is to write it yourself. You could do it in the following way:

    1. Create <div id="?">...</div> and populate with relevant text for all your options (change id="?" appropriately)
    2. Show only the default one and hide the other
    • if you are using bootstrap[345] you can apply classes (d-none vs d-block)
    • write your own javascript
    1. Then tie a "click" on the dropdown method to the activation of corresponding <div>

    With the amount of information you supplied us with, this is the most I can suggest to you. I hope I interpreted your question correctly.