1 year ago

#301572

test-img

Jack

Html - Create dropdown menu links from logged in user's name

I would like to create a dropdown menu from the logged-in user account. currently, the top-right button in the navigation bar is either "Login" or "Logout" based on if user is logged in. I would hope to create a dropdown menu when user is logged in, which contains more options such as account, myorder, and logout. So I can save space in the navigation bar by avoiding having too many buttons. Some desired examples like below:

enter image description here enter image description here enter image description here

The dropdown menu can contain two options (MyOrder and Logout). Please do not provide the external stylesheet as it will conflict/mess up my current page.

CSS

<style>
      .block [for=s1]{
        width: 400px;
        display: block;
        margin:5px 0;
        }
      .block [for=s2]{
        width: 800px;
        display: block;
        margin:5px 0;
        }
      .center [for=s1]{
        text-align: center;
        }
      .center [for=s2]{
        text-align: center;
        }

        label[for=s1]{
        display: inline-block;
        width: 100px;
        text-align: right;
        }
        label[for=s2]{
        display: inline-block;
        width: 70px;
        text-align: left;
        }

        input,textarea [for=s1]{
        vertical-align: top;
        }
        input,textarea [for=s2]{
        vertical-align: left;
        }

        .page-header {
        //background-color: #5E5A80;
        background-color: #454545;
        margin-top: 0;
        //padding: 20px 20px 20px 40px;
        padding: 5px 5px 5px 5px;
        }

        .page-header h1,
        .page-header h1 a,
        .page-header h1 a:visited,
        .page-header h1 a:active {
        color: #ffffff;
        font-size: 25pt;
        text-decoration: none;
        }

        input:focus{
            border-color: #66afe9;
            outline: 0;
            -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);
            box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6)
        }

        body{
            background-color: #F4F4F4;
        }
    </style>

    <style>
          * {
            box-sizing: border-box;
          }
          body {
            margin: 0;
          }
          .nav {
            overflow: hidden;
            background-color: #939596;
            //background-color: #454545;
            //background-color: #808080;
          }
          .nav a {
            float: left;
            display: block;
            color: #ffffff;
            text-align: center;
            padding: 5px 16px;
            text-decoration: none;
            font-size: 18px;
          }
                .nav span {
            float: left;
            display: block;
            color: #ffffff;
            text-align: center;
            //padding: 45px 16px;
            padding-top: 40px;
            padding-right: 16px;
            padding-bottom: 20px;
            //padding-left: 80px;
            text-decoration: none;
            font-size: 13px;
          }
    
            .nav h1,
            .nav h1 a,
            .nav h1 a:visited,
            .nav h1 a:active {
            color: #ffffff;
            font-size: 25pt;
            text-decoration: none;
            }
    
          .nav a:hover {
            background-color: #eeeeee;
            color: #000000;
          }
          .nav a.active {
            background-color: #0e002b;
            color: #ffffff;
          }
          .nav .login-container {
            float: right;
          }
          .nav input[type=text],
          .nav input[type=password] {
            padding: 6px;
            margin-top: 8px;
            font-size: 17px;
            border: none;
            width: 120px;
          }
          .nav .login-container button {
            float: right;
            padding: 6px 10px;
            margin-top: 33px;
            margin-right: 16px;
            background-color: #1c87c9;
            color: white;
            font-size: 17px;
            border: none;
            cursor: pointer;
          }
          .nav .login-container button:hover {
            background-color: #0e002b;
          }
          @media screen and (max-width: 600px) {
            .nav .login-container {
              float: none;
            }
            .nav a,
            .topnav input[type=text],
            .nav .login-container button {
              float: none;
              display: block;
              text-align: left;
              width: 100%;
              margin: 0;
              padding: 14px;
            }
            .nav input[type=text] {
              border: 1px solid #ccc;
            }
          }
    
        #AlertDiv {
        text-align:center;
        display: inline-block
    
        }
    
        #AlertDiv h1 {
        margin:auto;
        //vertical-align:middle;
             padding-top: 25px;
            padding-left: 300px;
            padding-bottom: 25px;
            text-align:center;
        }
        </style>
    
    
    
    
    
    
    <style>
    .dropbtn {
      background-color: #4CAF50;
      color: white;
      padding: 16px;
      font-size: 16px;
      border: none;
      cursor: pointer;
    }
    
    .dropdown {
      position: relative;
      display: inline-block;
    }
    
    .dropdown-content {
      display: none;
      position: absolute;
      background-color: #f9f9f9;
      min-width: 160px;
      box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
      z-index: 1;
    }
    
    .dropdown-content a {
      color: black;
      padding: 12px 16px;
      text-decoration: none;
      display: block;
    }
    
    .dropdown-content a:hover {background-color: #f1f1f1}
    
    .dropdown:hover .dropdown-content {
      display: block;
    }
    
    .dropdown:hover .dropbtn {
      background-color: #3e8e41;
    }
    </style>

HTML

   <div class="nav">
      <div class="login-container">
          {% if user.is_authenticated %}
        <form class="navbar-form navbar-right" action="{% url 'logout' %}" method="get">
            <span>Welcome, {{ user }}.&nbsp;&nbsp;</span>
          <button type="submit">logout</button>
            {% else %}
        <form class="navbar-form navbar-right" action="{% url 'login' %}" method="get">
            <span>Welcome, {{ user }}.&nbsp;&nbsp;</span>
          <button type="submit">login</button>
          {% endif %}
        </form>
      </div>
    </div>

html

css

django

drop-down-menu

navigationbar

0 Answers

Your Answer

Accepted video resources