2016-12-02 145 views
0

我与渲染与丑陋的“禁止进入”鼠标悬停图片(Firefox)的一个HTML输入控件,当输入框内文本被选中只读字段的ModelForm - 默认行为“models.TextField”字段呈现为输入我相信。的Django的ModelForm控件只读呈现文本不输入字段

self.fields['project_path'].widget.attrs['readonly'] = True 
self.fields['media_path'].widget.attrs['readonly'] = True 
self.fields['responsible'] = SysEventChoiceField(User.objects.all().order_by('first_name')) 

我想渲染只读领域,而不在表单中输入框纯文本的混合物的ModelForm。结果应该是这个样子的HTML ..

<p>D:\This\Path</p> 
<p>E:\This\other\path</p> 
<input value="Bob Smith" name="user"></input> 

回答

0

我不认为你可以取代从Django表单的<input>标签。相反,你可以得到你想显示为<p>和单独显示它们的字段。

在views.py

# get the fields you want to display as <p> 
my_field = form['my_field'].value() 

# remove from the form 
delete form['my_field'] 

# pass the parameters to render in your template 
return render_to_response('project.html', { 
    'my_field': my_field, 
    'form': form 
}) 

在你的模板project.html

<p>{{my_field}}</p> 
{{form}} 

或者,你也可以做这样的事情:

{% for field in form %} 
    {{ field.value }} 
{% endfor %} 


有同样的方法as_p(),但这只会w ^说唱在<p>标签,我想你不觉得你的输入字段。

+0

这也是我在做什么,但感觉就像我的思念像“self.fields [” media_path“]简单的东西。widget.attrs [” text_only'] = TRUE – Xeberdee