python20190116

1.管理系统基于form的增删改查

2.管理系统form表单的坑1

3.管理系统form表单的坑2

建立form_class.py,针对各个表单进行创建单独的form类
from django import forms
from django.forms import fields
from django.forms import widgets
from host import models

class UserForm(forms.Form):
    user = fields.CharField(error_messages={'required':'用户名不能为空'})
    # user = forms.CharField()
    pwd = fields.CharField(
        max_length=10,
        min_length=4,
        error_messages={'required':'用户名不能为空'},
        widget=widgets.PasswordInput(attrs={'class': 'form-contorl'})
    )
#注册form
class RegisterForm(forms.Form):
    username = forms.CharField(error_messages={'required': '用户名不能为空'})
    # user = forms.CharField()
    password = forms.CharField(
        max_length=10,
        min_length=3,
        error_messages={'required': '用户名不能为空'})
    pwd2 = forms.CharField(
        max_length=10,
        min_length=3,
        error_messages={'required': '用户名不能为空'})
    def clean(self):
        username = self.cleaned_data.get('username')
        models.UserInfo.objects.filter(username=username)
        try:
            pwd_confirm = self.cleaned_data.get('password')
            if self.cleaned_data.get('pwd2') == self.cleaned_data.get('password'):
                del self.cleaned_data['pwd2']
                return self.cleaned_data
            else:
                from django.core.exceptions import ValidationError
                self.add_error('pwd2', ValidationError('密码输入不一致'))
                return self.cleaned_data
        except KeyError:
            return self.cleaned_data
#资产列表form
class HostForm(forms.Form):
    hostname = fields.CharField(
        required=True,
        widget=widgets.TextInput(attrs={'class':'form-contorl'})
    )
    ipaddress = fields.GenericIPAddressField(
        required=True,
        error_messages={'required': '不能为空','invalid':'输入不合规'},
        widget=widgets.TextInput(attrs={'class': 'form-contorl'})
    )
    cpu = fields.IntegerField(
        required=True,
        widget=widgets.TextInput(attrs={'class': 'form-contorl'})
    )
    mem = fields.CharField(
        required=True,
        widget=widgets.TextInput(attrs={'class': 'form-contorl'})
    )
    disk = fields.CharField(
        required=True,
        widget=widgets.TextInput(attrs={'class': 'form-contorl'})
    )
    system = fields.CharField(
        required=True,
        widget=widgets.TextInput(attrs={'class': 'form-contorl'})
    )
    region_id = fields.ChoiceField(
        required=True,
        # initial=0,
        choices=[],
        widget=widgets.Select(attrs={'class': 'form-control'})
    )
    computer_room_id=fields.ChoiceField(
        required=True,
        # initial=0,
        choices=[],
        widget=widgets.Select(attrs={'class': 'form-control'})
    )

    # computer_room
    def __init__(self, *args, **kwargs):
        super(HostForm, self).__init__(*args, **kwargs)
        # print(models.ComputerRoom.objects.values_list('id', 'name'))
        self.fields['computer_room_id'].choices = models.ComputerRoom.objects.values_list('id', 'name')
        self.fields['region_id'].choices = models.Region.objects.values_list('id', 'name')
        # print('-----------',models.Region.objects.values_list('id', 'name'))
        ```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值