python - Django 错误 - TemplateSyntaxError at/Could not parse the remainder : '[' userType' ]' from ' ['userType' ]'

标签 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})

这是我的代码。我正在检查两个条件,但出现上述错误。谁能帮我解决这个问题?

最佳答案

通过键访问字典元素的内部模板使用:request.session.userType

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

关于python - Django 错误 - TemplateSyntaxError at/Could not parse the remainder : '[' userType' ]' from ' ['userType' ]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48241548/

相关文章:

django - uwsgi:无法识别的选项 '--module=MyProject.wsgi:application'

django - 如何序列化二进制文件以用于 celery 任务

python - 如何将列表和用户号码传递给函数并让它显示列表中大于用户号码的所有号码?

jquery - 如何使用 jQuery 获取 HTML 元素的尺寸/位置?

html - PrimeFaces:布局完整页面设置为“false”导致页脚消失

html - 表格 td 元素在错误的列下换行

python - Django Admin 使用 save_model() 加密更改(或保存)时某些字段的值

python - Sklearn TfIdfVectorizer 删除包含所有停用词的文档

python - 为什么字符串不变成整数?

python - 值错误 : unconverted data remains: 02:05