Django错误 - TemplateSyntaxError at /无法解析余数:'''userType']'来自'''userType']'

时间:2018-01-13 15:58:17

标签: python html django

 {% if 'is_logged_in' in request.session %} {% if request.session['userType'] == 0 %} {% include 'headersuccess.html' %} {% else %}dfgh {% endif %}

这是我的代码。我正在检查两个条件但是我收到了上述错误。有人可以帮我解决一下吗?

我的views.py是

@csrf_exempt
def index(request):
    if('is_logged_in' in request.session):
        id = request.session['authToken'];
        return render(request,"index.html",{'uid':id});
    else:
        return render(request,"index.html",{});

views.py(服务器)

这是我的服务器代码,在request.session中存在userType

@csrf_exempt
def login_user(request):
    user = None
    try:
        #user = models.user.objects.get(Q(user_name=request.POST['key'])|Q(contact__email=request.POST['key']))#|Q(contact__phone=request.POST['key']))
        user = models.user.objects.get(Q(user_name=request.POST['key'])|Q(email=request.POST['key']))#|Q(contact__phone=request.POST['key']))

        pswd = pass_enc(request.POST['password'],user.token)

        if user.password == pswd:

            #create signed auth token
            data = {
                'uid' : user.uid,
                'userType' : user.userType,
                'is_agent' : user.is_agent,
                'is_new'   : user.is_new,
                'is_admin' : user.userType == 8,
                'created_at':time.time()
            }

            signedToken = create_crypt_header(data)

            session = None

            try:
                session = models.session.objects.get(user=user)
                session.activeSession += 1
                session.save()
            except Exception as e:
                session = models.session(
                    user=user,
                    activeSession=1
                )
                session.save()
            #handel weblogin

            ajax_token = create_crypt_header({'val':1,'at':time.time()})

            request.session['authToken'] = signedToken
            request.session['v'] = 1
            request.session['ajax_token'] = ajax_token
            request.session['is_logged_in'] = True

            return JsonResponse({'authentication' : True,'authToken':signedToken,"userType":user.userType,'session_mask':session.sessionId,'is_new':user.is_new})
        else:
            return JsonResponse({'authentication' : False})

    except Exception as e:
        return JsonResponse({'authentication': False})

这是我的代码。我正在检查两个条件但是我收到了上述错误。有人可以帮我解决一下吗?

1 个答案:

答案 0 :(得分:1)

通过密钥使用full_graph()访问dict元素的内部模板:.

如果使用一个,你也不需要两个:request.session.userType。如果您的代码中有两个{% if 'is_logged_in' in request.session and request.session.userType == 0 %}块,则需要添加两个if语句。