如何在Django中返回并在模板上显示用户名和组

时间:2019-07-18 06:11:11

标签: django templates authentication

我有一个使用django的登录系统,我希望能够在包含导航的基本模板中显示属于它的登录用户酒吧。

直到现在,我可以返回用户名和组,并在控制台中打印出来。并在模板中显示用户名,但不包含组

views.py

def login_request(request):
    if request.method == "POST":
        username = request.POST['lognName']
        password = request.POST['lognCode']
        user = authenticate(username = username,password = password)
        if user.is_authenticated:
            print("user is authenticated", {username})
        else:
            print("user is NOT authenticated", {username})
        currentU = User.objects.filter(username=username).first()
        currentID = currentU.id
        print('current user is : ',currentID)
        groupName=User.objects.all().get(pk=currentID).groups.get().name
        print('set done ')
        print('the Groups Name {0}'.format(groupName))
        if user is not None:
            login(request,user)
            messages.info(request,f"You are now logged in as {username}")
            return redirect("create")
        else:
            messages.error(request,"invalid username or password")
            print("invalid username or password")

    return render(request,'login.html')

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


  <div id="container" class="Horizontal-menu">
    <ul>
        <li><a href="#">{{ user }} || {{ groupName }}</a></li>
            <li><a href="{% url 'logout' %}" id="logMeOut">logout</a></li>
            <li><a href="#">search</a></li>
            <li><a href="{%  url 'list'  %}">list members</a></li>
            <li><a href="{%  url 'create'  %}">mainpage</a></li>
             </th>
    </ul>

</script>
</body>

{% block body %}

{% endblock %}

</html>

2 个答案:

答案 0 :(得分:0)

您可以看到表格official documents,Django的内置 string reportName = "TestReport"; string displayName = reportName; bool isDomainAuthentication = false; var reportParameters = new List<ReportParameter>(); reportParameters.AddRange((List<ReportParameter>)Session["ReportParameters"]); string ReportServerURL = "http://DESKTOP38/ReportServer_SQLEXPRESS"; reportViewer1.ServerReport.ReportServerUrl = new Uri(ReportServerURL); reportViewer1.ServerReport.ReportPath = "FolderName" + reportName; reportViewer1.ServerReport.DisplayName = displayName; reportViewer1.ShowPrintButton = true; reportViewer1.ShowToolBar = true; reportViewer1.ShowCredentialPrompts = false; if (!isDomainAuthentication) { string userId = "DESKTOP38"; //Username string password = "test"; string domain = ReportServerURL; IReportServerCredentials reportCredentials = new ReportServerCredentials(userId, password, domain); reportViewer1.ServerReport.ReportServerCredentials = reportCredentials; } if (reportParameters != null) { reportViewer1.ServerReport.SetParameters(reportParameters); } this.reportViewer1.ServerReport.Refresh(); 模型与User模型具有ManyToMany关系。您可以按Group访问用户组,但是由于可能有一个用户分配了多个组,因此您需要使用user.groups循环类型的表达式来显示它。

for

答案 1 :(得分:-1)

您可以使用此

{{user.groups.all.0.name}}