0
我是一個noobs :)我試圖使用@Rune在這篇文章In a django form, How to make a field readonly (or disabled) so that it cannot be edited?答案之一,但我有一個錯誤,如標題中所述。如何解決這個錯誤在Django - 未定義的變量:etree
任何人都可以幫助我。我曾嘗試使用
從LXML進口etree - 得到錯誤未解決進口:etree
這
從xml.etree進口的ElementTree作爲etree - 擺脫錯誤未定義的變量進口:formstring
編碼
class ReadOnlyWidget(forms.Select):
def __init__(self, initial_widget, *args, **kwargs):
self.initial_widget = initial_widget
super(ReadOnlyWidget, self).__init__(*args, **kwargs)
def render(self, *args, **kwargs):
def guess_readonly_text(original_content):
root = etree.formstring("<span>%s</span>" % original_content)
for element in root:
if element.tag == 'input':
return element.get('value')
if element.tag == 'select':
for option in element:
if option.get('selected'):
return option.text
if element.tag == 'textarea':
return element.text
return "N/A"
original_content = self.initial_widget.render(*args, **kwargs)
try:
readonly_text = self.initial_widget.render_readonly(*args, **kwargs)
except AttributeError:
readonly_text = guess_readonly_text(original_content)
return mark_safe("""<span class="hidden">%s</span>%s""" %(original_content, readonly_text))
預先感謝您。
oppsss ....抱歉沒有意識到....非常感謝你@Daniel ......我修正了它 – noobes