2013-04-18 2 views
2

나는 도움이 필요하다. 내 장고 응용 프로그램에서 , 나는이 코드를 가지고 : 그러나python/django : 가져 오기 이름 충돌을 감지하고 피하는 방법은 무엇입니까?

from django.template import Context 

render_dict = {'scan': oval_scan, 'user': user, 'vulns': oval_vulns, 'asset_vulns': asset_vulns} 
report_html = get_template('oval_report.html').render(Context(render_dict)) 

을, 장고 나에게 다음과 같은 오류를 준 :

Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 111, in get_response 
    response = callback(request, *callback_args, **callback_kwargs) 
    File "/home/nopsec/nopsecvrm/apps/pegasus/views.py", line 2359, in ovalReport 
    report_html = get_template('pegasus/oval_report.html').render(Context(render_dict)) 
    File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 121, in render 
    context.render_context.push() 
AttributeError: 'Context' object has no attribute 'render_context' 
나는 다른에 다른 곳에서 또 다른 Context가 있기 때문에 한번이 오류를 만난 것을 기억

그래서 이런 식으로 코드를 변경, 실수로 사용 (매우 추한하지만 작동) 된 수입 패키지 :

import django 

render_dict = {'scan': oval_scan, 'user': user, 'vulns': oval_vulns, 'asset_vulns': asset_vulns} 
report_html = get_template('report.html').render(django.template.Context(render_dict)) 

내 질문은 : 어떻게 내가 할 수있는 어떤 Context django 실수로 추적 오류를보고 사용했는지 결정? 이 상황을 어떻게 해결할 수 있습니까? 감사.

답변

4

하나의 솔루션으로 가져온 Context 앨리어싱하여 충돌을 방지하는 것입니다 :

from django.template import Context as template_context 

그런 다음 당신이 사용하려는 버전을 필요로 할 때 template_context을 참조하십시오.

+0

을! –

0

__builtins__ 모듈에서 __import__ 기능을 사용하여 매우 좋다

#Detecting name conflicts: 
module_name_as_string = 'mymodule' 

if module_name_as_string in globals(): #we have name collision 
    try: 
     custom_module = __import__(module_name_as_string) 
    except ImportError: 
     pass 
관련 문제