Search code examples
pythonhtmlcssdjangomaterialize

Django how to assign toggle-switch/checkbox state to a variable and pass it to views.py?


There is a dark mode toggle-switch at the navbar, how can I pass it’s value views.py so it sets the template-name to page-dark.html or page-light.html according to the state of the toggle switch

This is my navbar:

<div class="navbar-fixed">
  <nav>
      <div class="nav-wrapper  teal lighten-1">
        <a href="#!" class="brand-logo"><i class="material-icons">assignment </i>Ai-Mo Times</a>
        <a href="#" data-target="mobile" class="sidenav-trigger"><i class="material-icons">menu</i></a>
        <ul class="right hide-on-med-and-down">
          <li><a href="/">Home</a></li>
          <li><a href="/newsroom">Newsroom</a></li>
          <li><div class="switch">
            <label>
            Off
           <input type="checkbox">
           <span class="lever"></span>
           On
           </label>
           </div></li>
        </ul>
      </div>
    </nav>
  </div>

How can achieve this, in case you need to know I’m using Materialize CSS

Thanks from now


Solution

  • You can use JS:

    <input type="checkbox" id="xxx" name="xxx" onclick="calc();"/>
    

    and in your JS code:

    <script>
    
    function calc()
    {
      if (document.getElementById('xxx').checked) 
      {
          // send to my view ON
      } else {
          // send to my view OFF
      }
    }
    
    </script>