如何将数据从视图发送到标题或导航栏

时间:2019-07-11 15:20:54

标签: django view django-templates

我有一个带有导航栏的Django网站,我需要在其中显示用户以及该用户所属的组,以使此信息在所有页面上可见。

直到现在,我都可以将这些数据项发送到特定页面,但不能发送到页眉。

views.py

def home(request):

 #print(User.groups.get())
 print(request.session.keys())
 if request.session.has_key('theUser'):
  authed=request.session['theUser']
  if request.session.has_key('group'):
   sections=request.session['group']
   return render(request,'./mainpage.html',{'sections':sections,'authed':authed})
  else:
   authed=None
 else:
  authed=None

 return render(request,'./Login.html',{'authed':authed})

我要发送 {'sections':sections,'authed':authed} 的位置 到 mainpage.html

mainpage.html

<div id='left-column-Input' class="formInput" include="select()">
        {{user.get_username}}|{{sections}}

我需要将数据发送到标头或基本文件

base.html

<!DOCTYPE html>
{% load static %}

<html>

    <head>
        <script type="text/javascript" src="{% static '/js/jquery-3.1.1.min.js' %}"></script>
        <link rel= "icon" type= "image/png" href="{% static 'img/logo_title/icon-AddressBar.png'%}">
        <link rel="stylesheet" type="text/css" href="{% static '/css/search.css'%}">
        <link rel="stylesheet" type="text/css" href="{% static '/css/style.css'%}">
    </head>
<body>
    <img class="logoMM" src="{% static 'img/logoMM.png' %}" alt="Smiley face" />
    <!-- <div class="wrap"> -->
   <div class="wrap search Horizontal-scale">
   <!-- <div class="Horizontal-scale"> -->
      <button type="submit" class="searchButton">
        <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="search"  role="img" viewBox="0 -20 530 530"><path fill="currentColor" d="M505 442.7L405.3 343c-4.5-4.5-10.6-7-17-7H372c27.6-35.3 44-79.7 44-128C416 93.1 322.9 0 208 0S0 93.1 0 208s93.1 208 208 208c48.3 0 92.7-16.4 128-44v16.3c0 6.4 2.5 12.5 7 17l99.7 99.7c9.4 9.4 24.6 9.4 33.9 0l28.3-28.3c9.4-9.4 9.4-24.6.1-34zM208 336c-70.7 0-128-57.2-128-128 0-70.7 57.2-128 128-128 70.7 0 128 57.2 128 128 0 70.7-57.2 128-128 128z"></path></svg>
     </button>
     <input type="text" class="searchTerm" placeholder="search">

    <!-- <div class="Horizontal-scale"> -->
    <label for="toggle">&#9776;</label>
    <input type="checkbox" id="toggle"/>

  <div id="container" class="Horizontal-menu">
    <ul>
      <li><a href="#">main</a></li>
      <li><a href="#">search</a></li>
               {% if authed is not None %}

        <li><a href="#" id="logMeOut">logout</a></li>
                <script type="text/javascript">
                    $(function(){
                        $('#logMeOut').on('click',function(){
                            $.ajax({
                                url:'/usr/logMeOut',
                                method:'POST',
                                headers:{
                                    'X-CSRFToken':'{{csrf_token}}'
                                }
                            }).done(function(msg){
                                console.log(msg)
                                document.location='/'
                            }).fail(function(err){
                                alert(err)
                            })
                        })
                    })

                </script>

            {% endif %} </th>

    </ul>

        </div>
</body>

{% block body %}

{% endblock %}

</html>

0 个答案:

没有答案