上一篇文章我已经新建了一个SaAdmin的APP,现在开始在这APP下面来code
1、修改setting.py的数据库连接:
1
2
3
4
5
6
7
8
9
10
|
DATABASES
=
{
'default'
: {
'ENGINE'
:
'django.db.backends.mysql'
,
'NAME'
:
'QjshAdmin'
,
'USER'
:
'root'
,
'PASSWORD'
:
'lihuipeng'
,
'HOST'
:
'localhost'
,
'PORT'
:
'3306'
,
}
}
|
新建数据库:
1
|
CREATE DATABASE QjshAdmin DEFAULT CHARACTER
SET
utf8 COLLATE utf8_general_ci;
|
2、建立表结构model.py:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
from
django.db
import
models
# Create your models here.
class
ServerList(models.Model):
kaifu_id
=
models.IntegerField()
plat
=
models.CharField(max_length
=
30
)
server_id
=
models.IntegerField()
server_name
=
models.CharField(max_length
=
30
)
open_time
=
models.DateTimeField()
domain
=
models.CharField(max_length
=
50
)
dx_ip
=
models.IPAddressField()
lt_ip
=
models.IPAddressField()
version
=
models.CharField(max_length
=
10
)
dfid
=
models.IntegerField()
hefu_range
=
models.TextField()
|
3、同步至数据库:
1
|
python manage.py syncdb
|
其间会提示输入超级管理员的一些信息,完成之后数据库会自动生成好多张表:
auth开头的是验证系统,暂时没用;django开头的是django功能表不用管,我只用到SaAdmin_serverlist这张表就OK
4、更新数据
好吧为了我不用添加两次后台,我叫技术大哥给我生成一个URL列表,然后我可以直接读取列表将信息插入数据库,毕竟我这玩意是做出来玩的,URL内容是这样:
views.py:
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
|
from
django.shortcuts
import
render
from
django.http.response
import
HttpResponse
from
django.views.generic
import
View
#model
from
models
import
*
#python
import
urllib2
# Create your views here.
class
Update_ServerList(View):
url
=
'http://xxx.xxx.xxx.com/qjsh/api/saadmin/'
def
get(
self
, request):
ServerList.objects.
all
().delete()
response
=
urllib2.urlopen(
self
.url)
for
line
in
response:
serverinfo
=
line.split(
','
)
SL
=
ServerList(kaifu_id
=
serverinfo[
0
],
plat
=
serverinfo[
1
],
server_id
=
serverinfo[
2
],
server_name
=
serverinfo[
3
],
open_time
=
serverinfo[
4
],
domain
=
serverinfo[
5
],
dx_ip
=
serverinfo[
6
],
lt_ip
=
serverinfo[
7
],
version
=
serverinfo[
8
],
dfid
=
serverinfo[
9
],
hefu_range
=
serverinfo[
10
])
SL.save()
return
HttpResponse(
"OK!"
)
|
插入后先简单返回个OK,我前端HTML都没写呢。。
接着配置urls.py:
1
2
3
4
5
6
7
8
9
10
|
from
django.conf.urls
import
patterns, include, url
from
django.views.generic
import
TemplateView
from
views
import
*
urlpatterns
=
patterns('',
url(r
'^test/$'
, TemplateView.as_view(template_name
=
'base2.jinja.html'
)),
url(r
'update/$'
, Update_ServerList.as_view(), name
=
"update_serverlist"
),
)
|
好了,启动服务器,访问http://127.0.0.1:8000/saadmin/update/先来看看效果,如无意外应该是显示OK的
现在基础数据也有了,下面该做什么呢。。
本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/1435983如需转载请自行联系原作者
lihuipeng