about 2 years ago

在一个文字框里加入灰色的注释文字帮助用户理解应该输入什么信息是常见的做法。在Django里有两种方式可以为输入框添加文字注释。假设我们有一个Model:

from django.db import models

class User(models.Model):
    user_name = models.CharField()

如果需要提示用户名应该是Email地址的话,有两种方法.

方法一:

import django.forms as forms
from models import User

class Login(forms.ModelForm):
    user_name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': u'输入Email地址'}))
    
    class Meta:
        model = User

方法二:

import django.forms as forms
from models import User

class Login(forms.ModelForm):
  
    class Meta:
        model = User
        widgets = {
             'user_name': forms.TextInput(attrs={'placeholder': u'输入Email地址'}),
        }
← 更好的编写Python代码的方式 国内快速安装gem的镜像服务器 →
 
comments powered by Disqus