Ex_treme's blog.

django前端快速部署

2018/05/18 Share

django搭建网站基础

配置数据库

1
2
3
4
5
6
7
8
9
10
11
12
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'zjk',
'USER':'root',
'PASSWORD':'root',
'HOST':'127.0.0.1',
'OPTIONS':{
"init_command":"SET foreign_key_checks=0;",
}
}
}

创建APP

1
2
3
4
#前端
manage.py@zjk > startapp web
#后端
导入xadmin

加载APP

1
2
3
4
5
6
7
8
9
10
11

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'web',
'xadmin',
]
1
2
3
#数据表反向生成model
manage.py@zjk > inspectdb table_name
编辑model

表迁移

1
2
manage.py@zjk > makemigrations
manage.py@zjk > migrate

url 解析

1
2
3
4
5
6
7
8
9
10
#views
def get(request):
return render(request,'index.html')
#urls
from web.views import get

urlpatterns = [
path('admin/', admin.site.urls),
path('',get)
]

静态网页配置

1
2
3
4
5
6
7
# setting
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
#html
{% load staticfiles %}
<link href="{% static 'css/style.css' %}" rel="stylesheet" type="text/css" />

动态显示数据

1
2
3
4
5
6
参考文档链接:https://blog.csdn.net/vic_torsun/article/details/69791170
#操作数据库的基础知识
>>> all_entries = Entry.objects.all()#检索所有对象
>>> fileter(**kwargs) #返回一个与参数匹配的QuerySet,相当于等于(=).
>>> exclude(**kwargs) #返回一个与参数不匹配的QuerySet,相当于不等于(!=)。
>>> >>> Entry.objects.all()[n]#这是查找第n个entry表里的数据

这次任务完成的是一个动态网页的部署,前端套用模板,从mysql中取数据,暂不做后台。重要文件全代码如下:

urls

1
2
3
4
5
6
7
8
9
10
11
from django.contrib import admin
from django.urls import path
from web.views import index,gys,zjk

urlpatterns = [
path('admin/', admin.site.urls),
path('',index),
path('gys/<int:id>/', gys),
path('zjk/<int:id>/', zjk),

]

model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from django.db import models

# Create your models here.
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# * Make sure each ForeignKey has `on_delete` set to the desired behavior.
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from django.db import models


class Zjk(models.Model):
xh = models.CharField(max_length=255, blank=True, null=True,verbose_name='序号')
id = models.CharField(max_length=255, blank=True,primary_key=True) # Field name made lowercase.
xm = models.CharField(max_length=255, blank=True, null=True ,verbose_name='姓名')
jsjb = models.CharField(max_length=255, blank=True, null=True,verbose_name='技术级别')
zjlb = models.CharField(max_length=255, blank=True, null=True,verbose_name='专家类别')
dwmc = models.CharField(max_length=255, blank=True, null=True,verbose_name='单位名称')
bgdh = models.CharField( max_length=255, blank=True, null=True,verbose_name='办公电话_军_field') # Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
sj = models.CharField(max_length=255, blank=True, null=True,verbose_name='手机')
zjcj = models.CharField(max_length=255, blank=True, null=True,verbose_name='专家层级')
zgxlsxzy = models.CharField(max_length=255, blank=True, null=True,verbose_name='最高学历所学专业')
kpsjshqzy1 = models.CharField(max_length=255, blank=True, null=True,verbose_name='可评审军事后勤专业1')
kpsjshqzy2 = models.CharField(max_length=255, blank=True, null=True,verbose_name='可评审军事后勤专业2')
kpsjshqzy3 = models.CharField(max_length=255, blank=True, null=True,verbose_name='可评审军事后勤专业3')
kpszylb = models.TextField(blank=True, null=True,verbose_name='可评审专业类别')

class Meta:
db_table = 'zjk'
# 表的别名
verbose_name = '专家库'
# 此项不定义则别名后会存在字符s(英文框架造成的)
verbose_name_plural = verbose_name

class Gys(models.Model):
xh = models.CharField(max_length=255, blank=True, null=True,verbose_name='序号')
id = models.CharField(max_length=255, blank=True, primary_key=True) # Field name made lowercase.
gsmc = models.CharField(max_length=255, blank=True, null=True,verbose_name='公司名称')
jjxz = models.CharField(max_length=255, blank=True, null=True,verbose_name='经济性质')
gyslx = models.CharField(max_length=255, blank=True, null=True,verbose_name='供应商类型')
gysdj = models.CharField(max_length=255, blank=True, null=True,verbose_name='供应商等级')
gsclsj = models.CharField(max_length=255, blank=True, null=True,verbose_name='公司成立时间')
lxrxm = models.CharField(max_length=255, blank=True, null=True,verbose_name='联系人姓名')
lxrsj = models.CharField(max_length=255, blank=True, null=True,verbose_name='联系人手机')
lxrgddh = models.CharField(max_length=255, blank=True, null=True,verbose_name='联系人固定电话')
lxrcz = models.CharField(max_length=255, blank=True, null=True,verbose_name='联系人传真')
fddbrxm = models.CharField(max_length=255, blank=True, null=True,verbose_name='法定代表人姓名')
fddbrgddh = models.CharField(max_length=255, blank=True, null=True,verbose_name='法定代表人固定电话')
fddbrsj = models.CharField(max_length=255, blank=True, null=True,verbose_name='法定代表人手机')
fddbrsfzh = models.CharField(max_length=255, blank=True, null=True,verbose_name='法定代表人身份证号')
lxryx = models.CharField(max_length=255, blank=True, null=True,verbose_name='联系人邮箱')
lxrdz = models.CharField(max_length=255, blank=True, null=True,verbose_name='联系人地址')
zzjgdm = models.CharField(max_length=255, blank=True, null=True,verbose_name='组织机构代码')
yzbm = models.CharField(max_length=255, blank=True, null=True,verbose_name='邮政编码')
zcdz = models.CharField(max_length=255, blank=True, null=True,verbose_name='注册地址')
gswz = models.CharField(max_length=255, blank=True, null=True,verbose_name='公司网址')
ssdq = models.CharField(max_length=255, blank=True, null=True,verbose_name='所属地区')
swdjh_gs = models.CharField(max_length=255, blank=True, null=True,verbose_name='税务登记证号_国税_field') # Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
swdjh_ds = models.CharField(max_length=255, blank=True, null=True,verbose_name='税务登记证号_地税_field') # Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
swdjfzjg_gs = models.CharField(max_length=255, blank=True, null=True,verbose_name='税务登记发证机关_国税_field') # Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
swdjfzjg_ds = models.CharField(max_length=255, blank=True, null=True,verbose_name='税务登记发证机关_地税_field') # Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
swdjyxq_gs = models.CharField( max_length=255, blank=True, null=True,verbose_name='税务登记证有效期_国税_field') # Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
swdjyxq_ds = models.CharField( max_length=255, blank=True, null=True,verbose_name='税务登记证有效期_地税_field') # Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
yyzzzch = models.CharField(max_length=255, blank=True, null=True,verbose_name='营业执照注册号')
yyzzfzjg = models.CharField(max_length=255, blank=True, null=True,verbose_name='营业执照发证机关')
yyzzzczj = models.CharField(max_length=255, blank=True, null=True,verbose_name='营业执照注册资金')
yyzzzcd = models.CharField(max_length=255, blank=True, null=True,verbose_name='营业执照注册地')
jyfw = models.TextField(blank=True, null=True,verbose_name='经营范围')
yyzzyxq = models.CharField(max_length=255, blank=True, null=True,verbose_name='营业执照有效期')
yyzzzjnjsj = models.CharField(max_length=255, blank=True, null=True,verbose_name='营业执照最近年检时间')
gsjj = models.TextField(blank=True, null=True,verbose_name='公司简介')
khyh = models.CharField(max_length=255, blank=True, null=True,verbose_name='开户银行')
yhzh = models.CharField(max_length=255, blank=True, null=True,verbose_name='银行账户')
ctbjl = models.TextField(blank=True, null=True,verbose_name='参投标经历')
zhtjsj = models.CharField(max_length=255, blank=True, null=True,verbose_name='最后提交时间')
cpml = models.TextField(blank=True, null=True,verbose_name='产品目录')

class Meta:
db_table = 'gys'
# 表的别名
verbose_name = '专家库'
# 此项不定义则别名后会存在字符s(英文框架造成的)
verbose_name_plural = verbose_name

views

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from django.shortcuts import render
from web.models import Zjk,Gys
from django.shortcuts import render_to_response

# Create your views here.
def index(request):
return render(request,'index.html')

def gys(request, id=0):
rlist = Gys.objects.all()[id]
gys = {"xh": rlist.xh, "id": rlist.id, "gsmc": rlist.gsmc, "jjxz": rlist.jjxz, "gyslx": rlist.gyslx,
"gysdj": rlist.gysdj, "gsclsj": rlist.gsclsj, "lxrxm": rlist.lxrxm, "lxrsj": rlist.lxrsj, "lxrgddh": rlist.lxrgddh,
"lxrcz": rlist.lxrcz, "fddbrxm": rlist.fddbrxm, "fddbrgddh": rlist.fddbrgddh,"fddbrsj": rlist.fddbrsj,
"fddbrsfzh": rlist.fddbrsfzh, "lxryx": rlist.lxryx, "lxrdz": rlist.lxrdz, "zzjgdm": rlist.zzjgdm,
"yzbm": rlist.yzbm, "zcdz": rlist.zcdz, "gswz": rlist.gswz, "ssdq": rlist.ssdq,"swdjh_gs": rlist.swdjh_gs,
"swdjh_ds": rlist.swdjh_ds, "swdjfzjg_gs": rlist.swdjfzjg_gs, "swdjfzjg_ds": rlist.swdjfzjg_ds, "swdjyxq_gs": rlist.swdjyxq_gs,
"swdjyxq_ds": rlist.swdjyxq_ds,
"yyzzzch": rlist.yyzzzch, "yyzzfzjg": rlist.yyzzfzjg, "yyzzzczj": rlist.yyzzzczj,"yyzzzcd": rlist.yyzzzcd,
"jyfw": rlist.jyfw, "yyzzyxq": rlist.yyzzyxq, "yyzzzjnjsj": rlist.yyzzzjnjsj,"gsjj": rlist.gsjj,
"khyh": rlist.khyh, "yhzh": rlist.yhzh, "ctbjl": rlist.ctbjl, "zhtjsj": rlist.zhtjsj,"cpml":rlist.cpml

}
res = render_to_response('gys.html', {'gys':gys})
return res

def zjk(request, id=0):
rlist = Zjk.objects.all()[id]
zjk = {"xh" : rlist.xh, "id" : rlist.id,"xm": rlist.xm,"jsjb": rlist.jsjb,"zjlb": rlist.zjlb,
"dwmc": rlist.dwmc,"bgdh": rlist.bgdh,"sj": rlist.sj,"zjcj": rlist.zjcj,"zgxlsxzy": rlist.zgxlsxzy,
"kpsjshqzy1": rlist.kpsjshqzy1,"kpsjshqzy2": rlist.kpsjshqzy2,"kpsjshqzy3": rlist.kpsjshqzy3,
"kpszylb": rlist.kpszylb,}
res = render_to_response('zjk.html', {'zjk':zjk})
return res

专家信息前端展示
image

供应商信息前端展示
image

CATALOG
  1. 1. django搭建网站基础
    1. 1.1. 配置数据库
    2. 1.2. 创建APP
    3. 1.3. 加载APP
    4. 1.4. modal
    5. 1.5. 表迁移
    6. 1.6. url 解析
    7. 1.7. 静态网页配置
    8. 1.8. 动态显示数据