六.1新增客户

添删一个功能时,都是从你的url开始写起.你的url就对应一个功能

1.先做展示;

(1).crm/urls.py中:

#增加客户
url(r'^customer/add/',views.add_customer,name='add_customer'),

(2).views.py中:

#增加客户
def add_customer(request):
return render(request,'crm/add_customer.html')

(3).templates/crm/add_customer.html:

{% extends 'layout.html' %}
{% block content %}

{% endblock %}

效果如下:这样模版就好了。

开始往它面板中添加内容部分,它的内容部分要用form表单---modelform实现

扫描二维码关注公众号,回复: 8205692 查看本文章

(4).开始写form,crm/forms.py中:----跟form相关的都写在此文件中

# 客户form
class CustomerForm(forms.ModelForm):
class Meta:#做配置用
model = models.Customer
fields = '__all__' #要显示的字段
widgets = { #此插件作用是把course课程字段改成多选框
'course': forms.widgets.SelectMultiple
}
def __init__(self, *args, **kwargs):#初始化--用来给多个插件做css样式
super().__init__(*args, **kwargs)#继承父类的初始法
for filed in self.fields.values():#给每一个插件
filed.widget.attrs.update({'class': 'form-control'}) #update是更新attrs插件字典的css样式

(5).开始使用form表单:先在views.py中导入它,并实例化对象再传给前端模版,前端模版中再渲染form即可

views.py中:

#增加客户
def add_customer(request):
#实例化一个空的form对象
form_obj = CustomerForm
return render(request,'crm/add_customer.html',{"form_obj":form_obj})

(6).add_customer.html中:将customer_list.html中面板拷贝到add_customer.html中并做个性修改:

{% extends 'layout.html' %}
{% block content %}
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">添加客户</div>
<div class="panel-body">
<div class="container">
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<form action="" method="post">
{% csrf_token %}
{% for field in form_obj %}

<div class="form-group row {% if field.errors %}has-error{% endif %} ">

<label for="{{ field.id_for_label }}"
class="col-sm-2 control-label"> {{ field.label }}</label>
<div class="col-sm-10">
{{ field }}
<span class="help-block">
{{ field.errors.0 }}
</span>
</div>
</div>
{% endfor %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">提交</button>
</div>
</div>
</form>
</div>
</div>
</div>

</div>
{% endblock %}

效果如下:显示就做完了!

 

 2.那怎么提交?

(1).

猜你喜欢

转载自www.cnblogs.com/dbslinux/p/12047760.html
今日推荐