社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

Django如何将页面的文本框设置成readonly?

oolocal • 8 年前 • 2827 次点击  
 
2827 次点击  
文章 [ 4 ]  |  最新文章 8 年前
oolocal
Reply   •   1 楼
oolocal    8 年前

@chengx2000 Setting the TextField to disable will cause the data cannot be submitted from a form, which is not good for me. Thank you though.

oolocal
Reply   •   2 楼
oolocal    8 年前

@Django中国社区 Thanks, this really helps. And actually I use another way to make the TextField readonly by setting the 'readonly' attribute in init() function: def init(self, *args, kwargs): super(forms.Form, self).__init__(*args,kwargs) self.fields['user_id'].widget.attrs['readonly'] = True self.fields['user_name'].widget.attrs['readonly'] = True self.fields['email'].widget.attrs['readonly'] = True

chengx2000
Reply   •   3 楼
chengx2000    8 年前

https://docs.djangoproject.com/en/1.10/ref/forms/fields/#disabled The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won’t be editable by users. Even if a user tampers with the field’s value submitted to the server, it will be ignored in favor of the value from the form’s initial data.

Py站长
Reply   •   4 楼
Py站长    8 年前
You should use a form field and not a model field:

somefield = models.CharField(
    widget=forms.TextInput(attrs={'readonly':'readonly'})
)
replaced with

somefield = forms.CharField(
    widget=forms.TextInput(attrs={'readonly':'readonly'})
)
Should fix it.

from https://www.sov5.com/search?q=readonly+django+text&re=1