1.建模型
迁移到数据库
注意,建数据库的时候要设置字符集
创建数据库指定编码格式
CREATE DATABASE test2
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
修改数据库字符集
alter database 数据库名 character set utf8;
修改表字符集
ALTER TABLE 表名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
python manage.py makemigrations appname
python manage.py migrate appname
2.配置url
3.写视图函数
(1)显示,继承ListView
class CustomerListView(AdminUserRequiredMixin, ListView):
# 跳转对应的模板
template_name = 'assets/customer_list.html'
# 对应的模型
model = Customer
# context_object_name 给前端传的参数名
context_object_name = 'customer_list'
# 分页
paginate_by = 5
# 给上下文添加参数,传给前端
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
'action': _('Customer list'),
}
kwargs.update(context)
return super().get_context_data(**kwargs)
(2)新建,继承CreateView
class CustomerCreateView(AdminUserRequiredMixin, CreateView):
model = Customer
template_name = 'assets/customer_create_update.html'
# 要写form,验证表单中的字段正确
form_class = CustomerForm
# 成功后跳转的页面
success_url = reverse_lazy('assets:customer-list')
success_message = create_success_msg
disable_name = ['draw', 'search', 'limit', 'offset', '_']
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
'action': _('Create customer'),
}
kwargs.update(context)
return super().get_context_data(**kwargs)
def form_valid(self, form):
name = form.cleaned_data.get('name')
if name in self.disable_name:
msg = _(
'Tips: Avoid using label names reserved internally: {}'
).format(', '.join(self.disable_name))
form.add_error("name", msg)
return self.form_invalid(form)
return super().form_valid(form)
(3)更新,继承UpdateView
class CustomerUpdateView(AdminUserRequiredMixin, UpdateView):
model = Customer
template_name = 'assets/customer_create_update.html'
form_class = CustomerForm
success_url = reverse_lazy('assets:customer-list')
success_message = update_success_msg
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
'action': _('Update customer'),
}
kwargs.update(context)
return super().get_context_data(**kwargs)
(4)删除,继承DeleteView
class CustomerDeleteView(AdminUserRequiredMixin, DeleteView):
model = Customer
template_name = 'delete_confirm.html'
success_url = reverse_lazy('assets:customer-list')
(5)一对多关联对象的显示
class CustomerAssetsView(AdminUserRequiredMixin, SingleObjectMixin, ListView):
paginate_by = settings.DISPLAY_PER_PAGE
template_name = 'assets/customer_assets.html'
context_object_name = 'assets'
model = Customer
object = None
# 首先进dispatch视图函数
def dispatch(self, request, *args, **kwargs):
self.pk = kwargs['pk']
return super().dispatch(request, *args, **kwargs)
def get_queryset(self):
queryset = Asset.objects.filter(customer_id=self.pk)
return queryset
def get_context_data(self, **kwargs):
context = {
'customer_id': self.pk
}
kwargs.update(context)
return super().get_context_data(**kwargs)
(6)ajax
<script>
$(document).ready(function(){
$('.delete-btn').click(function(){
var url = '{% url "assets:customer-delete" pk=DEFAULT_PK %}'.replace("{{ DEFAULT_PK }}", $(this).data('uid'))
console.log(url)
# 弹框的标签
swal({
title: "确定删除这条记录?",
icon: "warning",
buttons: true,
dangerMode: true,
}, function(){
$.ajax({
type: 'post',
url: url,
success: function(data){
location.reload();
}
})
})
})
})
</script>
网友评论