views.py
'''增加靚號''' from django.core.validators import RegexValidator class NumberModelForm(forms.ModelForm): # 重新定義一次 手機號的格式問題 mobile = forms.CharField( label='手機號', validators = [RegexValidator(r'^1[3-9]\d{9}$', '手機號格式錯誤')], ) class Meta: model = models.Number # 1. 方法1 fields = ['mobile', 'price', ....] 使用的字段 # 2. 方法2 exclude = ['mobile'] 排除的字段 # 3. 方法3 fields = '__all__' 全部字段 fields = '__all__' # 重定義方法 添加input 樣式 def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # 循環出所有字段, 並用插件 插入 clas 樣式 for name, field in self.fields.items(): # if name == 'name': # continue field.widget.attrs = {"class": "form-control", "placeholder": field.label} def number_add(request): if request.method =='GET': #實例化 form = NumberModelForm() return render(request, 'number_add.html', {'form': form}) # 校驗數據 form = NumberModelForm(data=request.POST) # 校驗數據 if form.is_valid(): print(form.cleaned_data) # 保存數據 form.save() return redirect('/number/list/') else: # 返回校驗錯誤信息 return render(request, 'number_add.html', {'form': form})
urls.py
# 手機號 path('number/list/', views.number_list), path('number/add/', views.number_add),
model.py
class Number(models.Model): '''手機靚號''' mobile = models.CharField(max_length=11, verbose_name='手機號碼') price = models.IntegerField(default=0, verbose_name='價格') ji_bie = { (1, '1級'), (2, '2級'), (3, '3級'), } ji = models.SmallIntegerField(choices=ji_bie, default=1, verbose_name='級別') zhuang_tai = { (1, '使用'), (2, '未使用'), } zhuang = models.SmallIntegerField(choices=zhuang_tai, default=2, verbose_name='狀態')
html 文件 number_add.html 文件代碼
{% extends 'layout.html' %} {% block content %} <div class="panel panel-default"> <div class="panel-heading"> <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> ModelfORM新建靚號</div> </div> <form method="post" novalidate> {% csrf_token %} {% for field in form %} <div class="form-group"> <label for="exampleInputEmail1"> {{ field.label }}</label> {{ field }} <span style="color: #f90">{{ field.errors.0 }}</span> </div> {% endfor %} <button type="submit" class="btn btn-primary">提交</button> </form> {% endblock%}
html文件 number_list.html 列表頁代碼
{% extends 'layout.html' %} {% block content %} <div style="margin-bottom: 10px;"> <a href="/number/add/" class="btn btn-success"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 新增靚號 </a> </div> <div class="panel panel-default"> <div class="panel-heading"> <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> 靚號列表</div> <table class="table table-bordered"> <thead> <tr> <th>ID</th> <th>號碼</th> <th>價格</th> <th>級別</th> <th>狀態</th> <th>操作</th> </tr> </thead> <tbody> {% for item in number_lits %} <tr> <th>{{ item.id }}</th> <td>{{ item.mobile }}</td> <td>{{ item.price }}</td> <td>{{ item.get_ji_display }}</td> <td>{{ item.get_zhuang_display }}</td> <td> <a href="/bumen/{{ item.id }}/edit" class="btn btn-primary btn-xs">編輯</a> <a href="/bumen/delete/?nid={{ item.id }}" class="btn btn-danger btn-xs">刪除</a> </td> </tr> {% endfor %} </tbody> </table> </div> {% endblock%}