在Django中呈现“ <class'django.template.response.TemplateResponse'>”对象

时间:2019-11-28 10:05:40

标签: python django saml openstack horizon

我需要使用python-saml将现有的可工作的openstack地平线与我们的SSO合并。

因此,我提到的演示文档写在这里:

https://github.com/onelogin/python-saml/blob/master/demo-django/demo/views.py#L113

因此,根据指南,我需要按照提到的方式呈现页面。

return render(request, 'auth/login.html', {'errors': errors, 'not_auth_warn': not_auth_warn, 'success_slo': success_slo, 'paint_logout': paint_logout, 'SSO': True})

当我尝试相同的操作时,准确地说,我没有在页面上得到预期的结果,我没有得到具有完整详细信息的确切页面。这是相同的更多细节。

预期的页面(这是我启用saml之前页面工作正常的屏幕截图):

correctop

现在处于实际页面(用户名和密码文本框不可见,因为login.html文件不足以呈现它,因为它需要完全显示表单,因此使用django_auth_views响应对象“ res”创建了相同的内容):< / p>

wrongop

代码:

def login(request, template_name=None, extra_context=None, **kwargs):
    """Logs a user in using the :class:`~openstack_auth.forms.Login` form."""

    if not request.is_ajax():.
        if (request.user.is_authenticated() and
                auth.REDIRECT_FIELD_NAME not in request.GET and
                auth.REDIRECT_FIELD_NAME not in request.POST):
            return shortcuts.redirect(settings.LOGIN_REDIRECT_URL)

    # Get our initial region for the form.
    initial = {}
    current_region = request.session.get('region_endpoint', None)
    requested_region = request.GET.get('region', None)
    regions = dict(getattr(settings, "AVAILABLE_REGIONS", []))
    if requested_region in regions and requested_region != current_region:
        initial.update({'region': requested_region})

    if request.method == "POST":
        if django.VERSION >= (1, 6):
            form = functional.curry(forms.Login)
        else:
            form = functional.curry(forms.Login, request)
    else:
        form = functional.curry(forms.Login, initial=initial)

    if extra_context is None:
        extra_context = {'redirect_field_name': auth.REDIRECT_FIELD_NAME}

    if not template_name:
        if request.is_ajax():
            template_name = 'auth/_login.html'
            extra_context['hide'] = True
        else:
            template_name = 'auth/login.html'

    res = django_auth_views.login(request,
                                  template_name=template_name,
                                  authentication_form=form,
                                  extra_context=extra_context,
                                  **kwargs)

    # Save the region in the cookie, this is used as the default
    # selected region next time the Login form loads.
    if request.method == "POST":
        utils.set_response_cookie(res, 'login_region',
                                  request.POST.get('region', ''))

    req = prepare_django_request(request)
    auth = init_saml_auth(req)
    errors = []
    not_auth_warn = False
    success_slo = False
    attributes = False
    paint_logout = False

    if 'sso' in req['get_data']:
        return HttpResponseRedirect(auth.login())

    elif 'slo' in req['get_data']:
        name_id = None
        session_index = None
        if 'samlNameId' in request.session:
            name_id = request.session['samlNameId']
        if 'samlSessionIndex' in request.session:
            session_index = request.session['samlSessionIndex']
        slo_built_url = auth.logout(name_id=name_id, session_index=session_index)
        request.session['LogoutRequestID'] = auth.get_last_request_id()
        print ('set logout id'+auth.get_last_request_id())
        return HttpResponseRedirect(slo_built_url)


    if 'samlUserdata' in request.session:
        paint_logout = True
        if len(request.session['samlUserdata']) > 0:
            attributes = request.session['samlUserdata'].items()

    return render(request, 'auth/login.html', {'errors': errors, 'not_auth_warn': not_auth_warn, 'success_slo': success_slo, 'paint_logout': paint_logout, 'SSO': True})


想知道这里可能是什么问题,什么可以解决此问题?

我给出了如下尝试,尝试返回在上述代码中创建的TemplateResponse对象,正确的工作代码只是从代码中返回“ res”对象。

return res

因此,我尝试返回对象而不是html文件。即:“ res”而不是“ auth / login.html”。

return render(request, res, {'errors': errors, 'not_auth_warn': not_auth_warn, 'success_slo': success_slo, 'paint_logout': paint_logout, 'SSO': True})

它返回错误如下:

Getting an error as follows:

    ContentNotRenderedError at /auth/login/
    The response content must be rendered before it can be accessed.

在分析过程中,我可以看到类型为 class'django.template.response.TemplateResponse'的模板对象(res)

请有人想出办法解决问题。

0 个答案:

没有答案