2016-12-02 146 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