第1章 CRM
1.1 建项目
1.2 settings
1.3 规范url
1.4 公共的后台模板
1.5 创建部门表
1.6 建库移库
1.7 母版继承
1.7.1 导入static
1.7.2 导入母版html
1.7.3 继承语法
第2章 部门
2.1 depart_list
2.1.1 列表url
2.1.2 创建函数
2.1.3 创建部门列表html
2.2 depart_add
2.2.1 crm.urls.py
2.2.2 创建MOdelForm
2.2.3 创建函数
2.2.4 创建depart_form.html
2.3 depart_del/depart_edit
2.3.1 结构目录
第3章 用户(一对多(增加MD5))
3.1 user_list
3.2 user_add
3.3 user_edit/user_del
第4章 课程(同部门略)
第5章 班级(多对多)
5.1 class_list
5.2 class_add
5.3 class_del/class_edit
第6章 分页及MD5
第7章 ModelForm
7.1 功能
7.2 生成html标签
7.2.1 fields = '__all__'
7.2.2 fields = ['title']
7.2.3 exclude = ['title']
7.3 instance

第1章 CRM

1.1 建项目

python3 manage.py  appstart crm

1.2 settings

pytho22.settings.py

INSTALLED_APPS = [

'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

'crm.apps.CrmConfig',

]

1.3 规范url

python22.crm.urls.py

from django.conf.urls import url,include

from django.contrib import admin

from crm.views import home

urlpatterns = [

url(r'^index/', home.index),

]

python22.urls.py

from django.conf.urls import url,include

from django.contrib import admin

urlpatterns = [

url(r'^admin/', admin.site.urls),

url(r'crm/', include('crm.urls')),

]

crm.views.home.py

from django.shortcuts import render

# Create your views here.

def index(request):

return  render(request,'index.html')

crm.templates.index.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>CRM:index</title>

</head>

<body>

<h1>CRM:index</h1>

</body>

</html>

1.4 公共的后台模板

模板的查找顺序,先找根目录,再由app注册的顺序查找

1.5 创建部门表

from django.db import models

# Create your models here.

class Department(models.Model):

'''

部门表

'''

title = models.CharField(verbose_name='部门表',max_length=32)

#将返回的object转换为字符串

def __str__(self):

return self.title

1.6 建库移库

python3 manage.py makemigrations

python3 manage.py migrate

1.7 母版继承

1.7.1 导入static

1.7.2 导入母版html

{% load staticfiles %}

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>路飞学城</title>

<link rel="shortcut icon" href="{% static 'imgs/luffy-study-logo.png' %} ">

<link rel="stylesheet" href="{% static 'plugins/bootstrap/css/bootstrap.css' %} "/>

<link rel="stylesheet" href="{% static 'plugins/font-awesome/css/font-awesome.css' %} "/>

<link rel="stylesheet" href="{% static 'css/commons.css' %} "/>

<link rel="stylesheet" href="{% static 'css/nav.css' %} "/>

<style>

body {

margin: 0;

}

.no-radius {

border-radius: 0;

}

.no-margin {

margin: 0;

}

.pg-body > .left-menu {

background-color: #EAEDF1;

position: absolute;

left: 1px;

top: 48px;

bottom: 0;

width: 220px;

border: 1px solid #EAEDF1;

overflow: auto;

}

.pg-body > .right-body {

position: absolute;

left: 225px;

right: 0;

top: 48px;

bottom: 0;

overflow: scroll;

border: 1px solid #ddd;

border-top: 0;

font-size: 13px;

min-width: 755px;

}

.navbar-right {

float: right !important;

margin-right: -15px;

}

.luffy-container {

padding: 15px;

}

.left-menu .menu-body .static-menu {

}

.left-menu .menu-body .static-menu .icon-wrap {

width: 20px;

display: inline-block;

text-align: center;

}

.left-menu .menu-body .static-menu a {

text-decoration: none;

padding: 8px 15px;

border-bottom: 1px solid #ccc;

color: #333;

display: block;

background: #efefef;

background: -webkit-gradient(linear, left bottom, left top, color-stop(0, #efefef), color-stop(1, #fafafa));

background: -ms-linear-gradient(bottom, #efefef, #fafafa);

background: -o-linear-gradient(bottom, #efefef, #fafafa);

filter: progid:dximagetransform.microsoft.gradient(startColorStr='#e3e3e3', EndColorStr='#ffffff');

-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorStr='#fafafa',EndColorStr='#efefef')";

box-shadow: inset 0 1px 1px white;

}

.left-menu .menu-body .static-menu a:hover {

color: #2F72AB;

border-left: 2px solid #2F72AB;

}

.left-menu .menu-body .static-menu a.active {

color: #2F72AB;

border-left: 2px solid #2F72AB;

}

</style>

{% block css %} {% endblock %}

</head>

<body>

<div class="pg-header">

<div class="nav">

<div class="logo-area left">

<a href="#">

<img class="logo" src="{% static 'imgs/logo.svg' %}">

<span style="font-size: 18px;">路飞学城 </span>

</a>

</div>

<div class="left-menu left">

<a class="menu-item">资产管理</a>

<a class="menu-item">用户信息</a>

<a class="menu-item">路飞管理</a>

<div class="menu-item">

<span>使用说明</span>

<i class="fa fa-caret-down" aria-hidden="true"></i>

<div class="more-info">

<a href="#" class="more-item">管他什么菜单</a>

<a href="#" class="more-item">实在是编不了</a>

</div>

</div>

</div>

<div class="right-menu right clearfix">

<div class="user-info right">

<a href="#" class="avatar">

<img class="img-circle" src="{% static 'imgs/default.png' %}">

</a>

<div class="more-info">

<a href="#" class="more-item">个人信息</a>

<a href="#" class="more-item">注销</a>

</div>

</div>

<a class="user-menu right">

消息

<i class="fa fa-commenting-o" aria-hidden="true"></i>

<span class="badge bg-success">2</span>

</a>

<a class="user-menu right">

通知

<i class="fa fa-envelope-o" aria-hidden="true"></i>

<span class="badge bg-success">2</span>

</a>

<a class="user-menu right">

任务

<i class="fa fa-bell-o" aria-hidden="true"></i>

<span class="badge bg-danger">4</span>

</a>

</div>

</div>

</div>

<div class="pg-body">

<div class="left-menu">

<div class="menu-body">

<div class="static-menu">

<a href="/crm/depart/list/"><span class="icon-wrap"><i class="fa fa-map-o" aria-hidden="true"></i></span> 部门管理</a>

</div>

</div>

</div>

<div class="right-body">

<div>

<ol class="breadcrumb no-radius no-margin" style="border-bottom: 1px solid #ddd;">

<li><a href="#">首页</a></li>

<li class="active">客户管理</li>

</ol>

</div>

{% block content %} {% endblock %}

</div>

</div>

<script src="{% static 'js/jquery-3.3.1.min.js' %} "></script>

<script src="{% static 'plugins/bootstrap/js/bootstrap.js' %} "></script>

{% block js %} {% endblock %}

</body>

</html>

1.7.3 继承语法

{% extends 'layout.html' %}

{% block content %}

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>CRM:index</title>

</head>

<body>

<h1>CRM:index</h1>

</body>

</html>

{% endblock %}

第2章 部门

2.1 depart_list

2.1.1 列表url

crm.urls.py

from django.conf.urls import url,include

from django.contrib import admin

from crm.views import home

from crm.views import  depart

urlpatterns = [

url(r'^index/', home.index),

url(r'^depart/list/', depart.depart_list)

2.1.2 创建函数

crm.views.depart.py

from django.shortcuts import  render

def depart_list(request):

return render(request,'depart_list.html')

2.1.3 创建部门列表html

crm.templates.depart_html

{% extends 'layout.html' %}

{% block content %}

<div style="margin: 5px;">

<a href="/crm/depart/add/" class="btn btn-primary"><i class="fa fa-plus-circle" aria-hidden="true"></i> 添加</a>

</div>

<table class="table table-bordered">

<thead>

<tr>

<th>ID</th>

<th>名称</th>

<th>操作</th>

</tr>

</thead>

<tbody>

{% for row in depart_queryset %}

<tr>

<td>{{ row.id }}</td>

<td>{{ row.title }}</td>

<td>

<a href="/crm/depart/edit/{{ row.id }}/"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>

|

<a href="/crm/depart/del/{{ row.id }}/" style="color: red;"><i class="fa fa-trash-o" aria-hidden="true"></i></a>

</td>

</tr>

{% endfor %}

</tbody>

</table>

<nav aria-label="Page navigation">

<ul class="pagination">

{{ pager.page_html|safe }}

</ul>

</nav>

{% endblock %}

2.2 depart_add

2.2.1 crm.urls.py

urlpatterns = [

url(r'^index/$', home.index),

url(r'^depart/list/$', depart.depart_list),

url(r'^depart/add/$', depart.depart_add),

]

2.2.2 创建MOdelForm

crm.forms.depart.py

from  crm  import  models

from  django.forms import ModelForm

from django import forms

class DepartModelForm(ModelForm):

class Meta:

model =  models.Department

fields = '__all__'

widgets = {

'title': forms.TextInput(attrs={'class':'form-control','placeholder':'部门名称'})

}

error_messages ={

'title':{

'required':'部门名称不能为空'

}

}

2.2.3 创建函数

crm.views.depart.py

def depart_add(request):

'''

添加部门

:param request:

:return:

'''

#批量创建部门

# for  i  in  range(103):

#     title = '部门-%s' %i

#     models.Department.objects.create(title=title)

if request.method == 'GET':

form = DepartModelForm()

return  render(request,'depart_add.html',{'form':form})

form = DepartModelForm(data=request.POST)

if form.is_valid():

form.save()

return redirect('/crm/depart/list/')

return render(request,'depart_form.html',{'form':form})

2.2.4 创建depart_form.html

crm.templates

{% extends 'layout.html' %}

{% block content %}

<form class="form-horizontal" method="post" novalidate>

{% csrf_token %}

<div class="form-group">

<label class="col-sm-2 control-label">{{ form.title.label }}</label>

<div class="col-sm-10">

{{ form.title }}

<span style="color: red;">{{ form.title.errors.0 }}</span>

</div>

</div>

<div class="form-group">

<div class="col-sm-offset-2 col-sm-10">

<input type="submit" class="btn btn-primary" value="提 交">

</div>

</div>

</form>

{% endblock %}

2.3 depart_del/depart_edit

crm.urls.py

url(r'^depart/edit/(\d+)/$', depart.depart_edit),

url(r'^depart/del/(\d+)/$', depart.depart_del),

crm.views.depart.py

def depart_edit(request,nid):

'''

编辑

:param request:

:return:

'''

obj = models.Department.objects.filter(id=nid).first()

if request.method == 'GET':

form = DepartModelForm(instance=obj)

return render(request,'depart_form.html',{'form':form})

form = DepartModelForm(data=request.POST,instance=obj)

if form.is_valid():

form.save()

return  redirect('/crm/depart/list/')

return  render(request,'depart_form.html',{'form':form})

@login_required

def  depart_del(request,nid):

models.Department.objects.filter(id=nid).delete()

return redirect('/crm/depart/list')

2.3.1 结构目录

第3章 用户(一对多(增加MD5))

3.1 user_list

crm.models.py

class UserInfo(models.Model):

"""

员工表

"""

username = models.CharField(verbose_name='用户名',max_length=32)

password = models.CharField(verbose_name='密码',max_length=64)

email = models.EmailField(verbose_name='邮箱',max_length=128)

gender_choices = (

(1,'男'),

(2,'女'),

)

gender = models.IntegerField(verbose_name='性别',choices=gender_choices)

depart = models.ForeignKey(verbose_name='所在部门',to='Department')

def __str__(self):

return self.username

crm.urls.py

url(r'^user/list/$', user.user_list),

crm.views.user.py

from  crm.forms.user import  UserModelForm

from  crm import models

from basic.pager import Pagination

from crm.views.login  import login_required

from basic.md5_mode import md5

@login_required

def user_list(request):

'''

用户列表

:param request:

:return:

'''

page = request.GET.get('page', 1)  # 要查看的页码

total_count = models.UserInfo.objects.all().count()  # 数据库中数据总条数

pager = Pagination(page, total_count, '/crm/user/list/')

user_queryset = models.UserInfo.objects.all()[pager.start:pager.end]

return render(request, 'user_list.html', {'user_queryset': user_queryset, 'pager': pager})

crm.templates.user_list.html

{% extends 'layout.html' %}

{% block content %}

<div style="margin: 5px;">

<a href="/crm/user/add/" class="btn btn-primary"><i class="fa fa-plus-circle" aria-hidden="true"></i> 添加</a>

</div>

<table class="table table-bordered">

<thead>

<tr>

<th>ID</th>

<th>姓名</th>

<th>密码</th>

<th>邮箱</th>

<th>性别</th>

<th>所属部门</th>

<th>操作</th>

</tr>

</thead>

<tbody>

{% for row in user_queryset %}

<tr>

<td>{{ row.id }}</td>

<td>{{ row.username }}</td>

<td>{{ row.password }}</td>

<td>{{ row.email }}</td>

{#性别选择,数据库中写好的选择方式,可直接由django调用#}

<td>{{ row.get_gender_display }}</td>

{#针对多表#}

<td>{{ row.depart.title }}</td>

<td>

<a href="/crm/user/edit/{{ row.id }}"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>

|

<a href="/crm/user/del/{{ row.id }}" style="color: red;"><i class="fa fa-trash-o" aria-hidden="true"></i></a>

</td>

</tr>

{% endfor %}

</tbody>

</table>

<nav aria-label="Page navigation">

<ul class="pagination">

{{ pager.page_html|safe }}

</ul>

</nav>

{% endblock %}

3.2 user_add

crm.urls.py

url(r'^user/add/$', user.user_add),

crm.forms.user.py

from django import forms

from  crm import models

class UserModelForm(forms.ModelForm):

class Meta:

model =  models.UserInfo

fields = '__all__'

widgets = {

'username': forms.TextInput(attrs={'class':'form-control','placeholder':'用户名'}),

'password': forms.TextInput(attrs={'class':'form-control','placeholder':'密码'}),

'email': forms.EmailInput(attrs={'class':'form-control','placeholder':'邮箱'}),

'gender': forms.Select(attrs={'class':'form-control'}),

'depart': forms.Select(attrs={'class':'form-control'}),

}

error_messages ={

'email':{

'required':'邮箱地址不能为空',

'invalid':'邮箱的格式错误',

},

'username':{

'required':'用户名称不能为空',

# 'invalid':'邮箱的格式错误',

},

'password':{

'required':'密码称不能为空',

# 'invalid':'邮箱的格式错误',

},

'gender': {

'required': '亲选择你的性别',

# 'invalid':'邮箱的格式错误',

},

'depart': {

'required': '请选择一个部门',

# 'invalid':'邮箱的格式错误',

},

}

crm.views.user.py

def user_add(request):

'''

增加用户

:param request:

:return:

'''

if request.method == 'GET':

form  = UserModelForm()

return render(request,'user_form.html',{'form':form})

form = UserModelForm(data=request.POST)

if form.is_valid():

username = form.cleaned_data['username']

pwd = form.cleaned_data['password']

email = form.cleaned_data['email']

gender = form.cleaned_data['gender']

depart = form.cleaned_data['depart']

md5pwd = md5(username,pwd)

models.UserInfo.objects.get_or_create(password=md5pwd,username=username,email=email,gender=gender,depart=depart)

# form.save()

return redirect('/crm/user/list')

return render(request,'user_form.html',{'form':form})

crm.templates.user_form.html

{% extends 'layout.html' %}

{% block content %}

<form class="form-horizontal" method="post" novalidate>

{% csrf_token %}

{% for field in form %}

<div class="form-group">

<label class="col-sm-2 control-label">{{ field.label }}</label>

<div class="col-sm-10">

{{ field }}

<span style="color: red;">{{ field.errors.0 }}</span>

</div>

</div>

{% endfor %}

<div class="form-group">

<div class="col-sm-offset-2 col-sm-10">

<input type="submit" class="btn btn-primary" value="提 交">

</div>

</div>

</form>

{% endblock %}

3.3 user_edit/user_del

def user_edit(request,nid):

obj = models.UserInfo.objects.filter(id=nid).first()

if request.method== 'GET':

form = UserModelForm(instance=obj)

return render(request,'user_form.html',{'form':form})

form = UserModelForm(data=request.POST,instance=obj)

if form.is_valid():

form.save()

return redirect('/crm/user/list/')

return render(request,'user_form.html',{'form':form})

def user_del(request,nid):

models.UserInfo.objects.filter(id=nid).delete()

return redirect('/crm/user/list/')

第4章 课程(同部门略)

第5章 班级(多对多)

5.1 class_list

crm.models.py

class ClassList(models.Model):

"""

班级表

"""

course = models.ForeignKey(verbose_name='课程', to='Course')

num = models.IntegerField(verbose_name='期')

teachers = models.ManyToManyField(verbose_name='老师', to='UserInfo')

def __str__(self):

return self.course

crm.urls.py

url(r'^class/list/$', class_list.class_list),

crm.views.class_list.py

from django.shortcuts import  render,redirect

from  crm.forms.class_list import  ClassModelForm

from  crm import models

from basic.pager import Pagination

from crm.views.login  import login_required

@login_required

def class_list(request):

'''

课程列表

:param request:

:return:

'''

page = request.GET.get('page', 1)  # 要查看的页码

total_count = models.ClassList.objects.all().count()  # 数据库中数据总条数

pager = Pagination(page, total_count, '/crm/class/list/')

class_queryset = models.ClassList.objects.values('id','course__title','num','teachers__username')[pager.start:pager.end]

return render(request, 'class_list.html', {'class_queryset': class_queryset, 'pager': pager})

crm.templates.class_list.html

{% extends 'layout.html' %}

{% block content %}

<div style="margin: 5px;">

<a href="/crm/class/add/" class="btn btn-primary"><i class="fa fa-plus-circle" aria-hidden="true"></i> 添加</a>

</div>

<table class="table table-bordered">

<thead>

<tr>

<th>ID</th>

<th>课程</th>

<th>期数</th>

<th>导师</th>

<th>操作</th>

</tr>

</thead>

<tbody>

{% for row in class_queryset %}

<tr>

<td>{{ row.id}}</td>

<td>{{ row.course__title}}</td>

<td>{{ row.num }}</td>

<td>{{ row.teachers__username}}</td>

<td>

<a href="/crm/class/edit/{{ row.id }}"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>

|

<a href="/crm/class/del/{{ row.id }}" style="color: red;"><i class="fa fa-trash-o" aria-hidden="true"></i></a>

</td>

</tr>

{% endfor %}

</tbody>

</table>

<nav aria-label="Page navigation">

<ul class="pagination">

{{ pager.page_html|safe }}

</ul>

</nav>

{% endblock %}

5.2 class_add

crm.urls.py

url(r'^class/add/$', class_list.class_add),

crm.views.class_list.py

def class_add(request):

'''

增加班级

:param request:

:return:

'''

if request.method == 'GET':

form  = ClassModelForm()

return render(request,'class_form.html',{'form':form})

form = ClassModelForm(data=request.POST)

if form.is_valid():

form.save()

return redirect('/crm/class/list')

return render(request,'class_form.html',{'form':form})

crm.templates.class_form.html

{% extends 'layout.html' %}

{% block content %}

<form class="form-horizontal" method="post" novalidate>

{% csrf_token %}

{% for field in form %}

<div class="form-group">

<label class="col-sm-2 control-label">{{ field.label }}</label>

<div class="col-sm-10">

{{ field }}

<span style="color: red;">{{ field.errors.0 }}</span>

</div>

</div>

{% endfor %}

<div class="form-group">

<div class="col-sm-offset-2 col-sm-10">

<input type="submit" class="btn btn-primary" value="提 交">

</div>

</div>

</form>

{% endblock %}

crom.forms.class_list.py

from django import forms

from  crm import models

class ClassModelForm(forms.ModelForm):

class Meta:

model =  models.ClassList

fields = '__all__'

widgets = {

'course': forms.Select(attrs={'class':'form-control'}),

'num': forms.TextInput(attrs={'class':'form-control','placeholder':'期数'}),

'teachers': forms.SelectMultiple(attrs={'class':'form-control'}),

}

error_messages ={

'course':{

'required':'请至少选择一个课程',

},

'num':{

'required':'期数名称不能为空',

},

'teachers':{

'required':'请至少选择一名导师',

},

'gender': {

'required': '亲选择你的性别',

# 'invalid':'邮箱的格式错误',

},

'depart': {

'required': '请选择一个部门',

# 'invalid':'邮箱的格式错误',

},

}

5.3 class_del/class_edit

urls.py

url(r'^class/edit/(\d+)/$', class_list.class_edit),
url(r'^class/add/(\d+)/$',
class_list.class_del),

views.class.py

def class_edit(request,nid):

obj =
models.ClassList.objects.filter(id=nid).first()

if
request.method== 'GET':

form
= ClassModelForm(instance=obj)

return
render(request,'class_form.html',{'form':form})

form =
ClassModelForm(data=request.POST,instance=obj)

if
form.is_valid():

form.save()

return redirect('/crm/class/list/')

return
render(request,'class_form.html',{'form':form})

def class_del(request,nid):

models.ClassList.objects.filter(id=nid).delete()

return
redirect('/crm/class/list/')

第6章 分页及MD5

python22.basic.md5.py

import hashlib

#MD5加密

def md5(username,password):

'''

动态用户名加盐

:param
username:

:param password:

:return:

使用方法:md5(username,password)

'''

md5_pwd =
hashlib.md5(username.encode("utf-8"))

md5_pwd.update(password.encode('utf-8'))

#返回加密数据

return
md5_pwd.hexdigest()

python22.pager.py

"""

使用文档:

1. 获取两个数据给Pagination

- 数据库的数据总数据量

- 当前要访问的页码

例如:

page = request.GET.get('page', 1) # 要查看的页码

total_count = models.Department.objects.all().count() # 数据库中数据总条数

pager = Pagination(page,total_count,'/crm/depart/list/')

2. Pagination对象有两个功能

- 计算数据切片索引

例如:

depart_queryset = models.Department.objects.all()[pager.start
:pager.end]

- 生成页码HTMl

例如:在页面中使用

<ul class="pagination">

{{ pager.page_html|safe
}}

</ul>

"""

class Pagination(object):

"""

分页相关

"""

def
__init__(self,page,total_count,url, per_page_count=10,max_page_num=11):

"""

:param page: 当前页

:param total_count: 数据库中数据总条数

:param url: 路径

:param per_page_count: 页面显示的数据条数

:param max_page_num:  最多显示的页码数量

"""

try:

page = int(page)

except Exception as e:

page =1

if
page <1:

page = 1

self.page = page

self.total_count = total_count

self.url = url

self.per_page_count = per_page_count

self.max_page_num = max_page_num

self.half_max_page_num = int(max_page_num / 2)

self.total_page_num, x = divmod(total_count, per_page_count)

if x
> 0:

self.total_page_num += 1

@property

def
start(self):

return (self.page-1) * self.per_page_count

@property

def
end(self):

return self.page * self.per_page_count

def
page_html(self):

# 如果数据库中的数据最多总共能到11条 10,9,0 < max_page_num

if
self.total_page_num <= self.max_page_num:

page_start = 1

page_end = self.total_page_num + 1

else:

#
如果当前访问的页码: 1,2,3,4,5

if self.page <=self.half_max_page_num:

page_start = 1

page_end = self.max_page_num + 1

else:

# 如果当前页+5 > 总页码

if self.page + self.half_max_page_num > self.total_page_num:

page_start = self.total_page_num - self.max_page_num + 1

page_end = self.total_page_num + 1

else:

page_start = self.page - self.half_max_page_num

page_end = self.page + self.half_max_page_num + 1

pager_list = []

# 上一页

if
self.page > 1:

prev = '<li><a href="%s?page=%s">上一页</a></li>' % (self.url,self.page - 1,)

pager_list.append(prev)

for i
in range(page_start, page_end):  # 应该显示一定范围的页码

if i == self.page:

temp = '<li class="active"><a
href="%s?page=%s">%s</a></li>' % (self.url,i, i,)

else:

temp = '<li><a
href="%s?page=%s">%s</a></li>' % (self.url,i, i,)

pager_list.append(temp)

# 下一页

if
self.page < self.total_page_num:

nex = '<li><a href="%s?page=%s">下一页</a></li>' % (self.url,self.page + 1,)

pager_list.append(nex)

return "".join(pager_list)

第7章 ModelForm

7.1 功能


生成用户提交请求的html标签


提供用户提交请求校验方式

7.2 生成html标签

crm.forms.depart.py

from 
crm  import  models

from 
django.forms import ModelForm

from django import forms

class DepartModelForm(ModelForm):

#固定写法

class
Meta:

model
=  models.Department

#将数据库的值拿出来一个个进行对比校验

fields = '__all__'

#生成请求标签

widgets = {

'title': forms.TextInput(attrs={'class':'form-control','placeholder':'部门名称'})

}

error_messages ={

'title':{

'required':'部门名称不能为空'

}

}

html中的取值

7.2.1 fields =
'__all__'

解释:全部进行比对校验

7.2.2 fields =
['title']

解释:只数据库中的title,进行对比

7.2.3 exclude
= ['title']

解释:只对数据库中的title,进行排除

7.3 instance

携带默认值,数据库值出入form

def depart_edit(request,nid):

'''

编辑

:param
request:

:return:

'''

obj =
models.Department.objects.filter(id=nid).first()

if
request.method == 'GET':

#生成默认值+html标签,默认值包括obj所有数据

form
= DepartModelForm(instance=obj)

return render(request,'depart_form.html',{'form':form})#return form带默认值

form =
DepartModelForm(data=request.POST,instance=obj)

if
form.is_valid():

form.save()

return 
redirect('/crm/depart/list/')

return 
render(request,'depart_form.html',{'form':form})

python_22(Form-CRM)的更多相关文章

  1. C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序

    C#中缓存的使用   缓存的概念及优缺点在这里就不多做介绍,主要介绍一下使用的方法. 1.在ASP.NET中页面缓存的使用方法简单,只需要在aspx页的顶部加上一句声明即可:  <%@ Outp ...

  2. mvc报( 检测到有潜在危险的 request.form 值 )错的解决方案

    今天在做项目中遇到了报( 检测到有潜在危险的 request.form 值 )错,百度过后解决了该问题,出此问题主要还是因为提交的Form中有HTML字符串,例如你在TextBox中输入了html标签 ...

  3. antd快速开发(Form篇)

    antd快速开发(Form篇) 前言 由于一直在做中台业务,后台项目特别多,但是后台项目的特点是:大量的列表和大量表单,重复开发会降低效率,所以我这边总结了一下使用antd组件搭建form的快捷方法. ...

  4. UI标签库专题十:JEECG智能开发平台 Form(form标签)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/30099121  1. Form(f ...

  5. JavaScript常用内置对象(window、document、form对象)

    由于刚开始学习B/S编程,下面对各种脚本语言有一个宏观的简单认识. 脚本语言(JavaScript,Vbscript,JScript等)介于HTML和C,C++,Java,C#等编程语言之间.它的优势 ...

  6. 项目分享:通过使用SSH框架的公司-学员关系管理系统(CRM)

    ----------------------------------------------------------------------------------------------[版权申明: ...

  7. Django---form表单提交数据到数据库(普通方法+Django的form类)

    目标: ①.初始form的简单应用 ②.使用Django的form组件完成新增一个帖子 方法一:普通方法 1.前端表单代码 <div> <form class="navba ...

  8. Ajax提交form表单内容和文件(jQuery.form.js)

    jQuery官网是这样介绍form.js A simple way to AJAX-ify any form on your page; with file upload and progress s ...

  9. jQuery学习笔记(jquery.form插件)

    官网: http://malsup.com/jquery/form/ jQuery Form插件是一个优秀的Ajax表单插件,可以非常容易地.无侵入地升级HTML表单以支持Ajax.jQuery Fo ...

  10. linux项目部署(非前后端分离crm)

    参考博客 参考博客2---部署过程 导论:看参考博客1 WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器如何与Web应用程序通信,以及Web应用程序如何链接在一起以处理一个请求,(接收 ...

随机推荐

  1. 使用grunt js进行js的链接和压缩

    1,http://nodejs.org/download/ 安装nodejs 2,配置环境变量,将nodejs的安装目录放置在Path环境变量中 3,在cmd中 npm install -g grun ...

  2. AndroidManifest中的Intent-filter标签

    经过测试,intent-filter标签中的: 1. <action android:name="android.intent.action.MAIN" /> 代表这是 ...

  3. Lsyncd搭建同步镜像-用Lsyncd实现本地和远程服务器之间实时同步

    Lysncd即Live Syncing Daemon,它是开源的数据实时同步工具(后台进程),基于inotify和rsync. lsyncd会密切监测本地服务器上的参照目录,当发现目录下有文件或目录变 ...

  4. tabbar

    1 tabbar不显示的问题: 命名在app.json中配置了tabbar但是不显示可能的问题: app.json中的pages中第一个路径没有在tabbar中设置 原因:app.json中配置的pa ...

  5. c++中IO输入输出流总结<二>

    1 文件的打开和关闭 1.1 定义流对象 ifsteam iflie;//文件输入流对象 ifsteam iflie;//文件输出流对象 fsteam iflie;//文件输入输出流对象 1.2 打开 ...

  6. PHP中正则表达式学习及应用(二)

    正则表达式中的“元字符” * 匹配前一个内容的0次1次或多次 例如: <?php $mode="/go*gle/"; //前一个内容指的是 * 的前一个字符 o ,在$str ...

  7. LeetCode: 292 Nim Game(easy)

    题目: You are playing the following Nim Game with your friend: There is a heap of stones on the table, ...

  8. STL——stack

    首先,堆栈是一个线性表,插入和删除只在表的一端进行.这一端称为栈顶(Stack Top),另一端则为栈底(Stack Bottom).堆栈的元素插入称为入栈,元素的删除称为出栈.由于元素的入栈和出栈总 ...

  9. unity常用插件

    Unity3D常用插件,网址:http://jingyan.baidu.com/article/7f766daf4ef2844100e1d079.html ,想想自己也有小半年unity经验了,于是整 ...

  10. 51nod1625(枚举&贪心)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 题意:中文题诶- 思路:枚举+贪心 一开始写的行和列同时 ...