bootsrap.py 文件
from django import forms class BootstrapModelForm(forms.ModelForm): # 重定义方法 添加input 样式 def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # 循环出所有字段, 并用插件 插入 clas 样式 for name, field in self.fields.items(): # if name == 'name': # continue if field.widget.attrs: field.widget.attrs['class'] = 'form-control' field.widget.attrs['placeholder'] = field.label else: field.widget.attrs = {"class": "form-control", "placeholder": field.label}
添加用户 admin.py
from app.utils.bootsrap import BootstrapModelForm
class AdminAddModelForm(BootstrapModelForm): confirm_pwd = forms.CharField( label='重复密码', widget=forms.PasswordInput ) class Meta: model = models.Admin fields = '__all__' widgets = { 'pwd' : forms.PasswordInput, } '''管理员添加''' def admin_add(request): form = AdminAddModelForm() return render(request, 'change.html', {"title": '新建管理员', 'form': form})
其他继承案例 :
urls.py 代码
from app.views import admin urlpatterns = [ #管理员管理 path('admin/list/', admin.admin_list), path('admin/delete/', admin.admin_delete), path('admin/add/', admin.admin_add),