Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
模板中的变量(django)_Django_Python 3.x_Django Templates - Fatal编程技术网

模板中的变量(django)

模板中的变量(django),django,python-3.x,django-templates,Django,Python 3.x,Django Templates,我正在使用LDAP后端对用户进行身份验证。用户登录后,将显示以下视图: views.py: def index(request): return render(request, 'index.html') index.html包含以下部分: {% if user.is_authenticated %} <a href="/link1/">Link 1</a> | <a href="/link2/">Link 2</a> | <a

我正在使用LDAP后端对用户进行身份验证。用户登录后,将显示以下视图:

views.py:

def index(request):

    return render(request, 'index.html')
index.html包含以下部分:

{% if user.is_authenticated %}
<a href="/link1/">Link 1</a> | <a href="/link2/">Link 2</a> | <a href="/logout/">Logout</a>  ({{ user.username }})
{% else %}
<a href="/login/">Login</a><br />
{% endif %}

我想创建一个函数,在LDAP中搜索特定属性,并将结果作为列表返回。问题是,如果函数在ldap.py中,那么如何在模板文件中定义变量,如{{user.username}}。感谢您的帮助。

您应该将包含变量的上下文词典传递给模板。变量(如
user
)不会神奇地出现在模板中。它们通过上下文传递,无论是从您的视图还是通过中间件。谢谢。我就是这么做的,而且很管用。
from django.conf import settings
from ldap3 import Server, Connection, ALL, SYNC, SIMPLE, SUBTREE
from django.contrib.auth.models import User
from ldap3.core.exceptions import LDAPBindError


class LDAPBackend(object):

    def __init__(self):

        self.domain = settings.AD_DNS_NAME


    def authenticate(self, username = None, password = None):

        username = username.lower()
        if not self.is_valid(username, password):
            return None

        try:
            self.s = Server(self.domain, port = 636, use_ssl = True, get_info = ALL)
            self.c = Connection(self.s, auto_bind = True,
                            client_strategy = SYNC,
                            user = username, password = password,
                            authentication = SIMPLE,
                            check_names = True)

        except LDAPBindError:
            return False

        try:
            user = User.objects.get(username = username)           
        except User.DoesNotExist:
            result = self.c.extend.standard.paged_search(search_base = settings.AD_BASE_DN,
                        search_filter = "(sAMAccountName = %s)" % username,
                        search_scope = SUBTREE,
                        attributes = settings.AD_USER_ATTR,
                        paged_size = 5,
                        generator = False)

            if result[3]["attributes"]["givenName"]:
                first_name = result[3]["attributes"]["givenName"]
            else:
                first_name = None

            if result[3]["attributes"]["sn"]:
                last_name = result[3]["attributes"]["sn"]
            else:
                last_name = None

            if result[3]["attributes"]["mail"]:
                email = result[3]["attributes"]["mail"]
            else:
                email = None

            user = User(username = username,
                    first_name = first_name,
                    last_name = last_name,
                    email = email)

            user.is_staff = False
            user.is_superuser = False
            user.set_password(password)
            user.save()

    return user


def is_valid (self, username = None, password = None):

    if password == None or password == "":
        return False


def get_user(self, user_id):

    try:
        return User.objects.get(pk = user_id)
    except User.DoesNotExist:
        return None