pythonweb快速开发平台_30分钟快速搭建Web CRUD的管理平台--django神奇魔法

加上你的准备的时间,估计30分钟完全够用了,因为最近在做爬虫管理平台,想着快速开发,没想到python web平台下有这么非常方便的框架,简洁而优雅。将自己的一些坑总结出来,方便给大家的使用。

准备环境:

系统:win7 or ubuntu

django版本:1.8.5

python版本:2.7.6

数据库:自带的SQLLITE3

IDE: sublime text 3

===========================Read ? go===================================

一,选择文件夹,用命令行创建文件夹

sudo django-admin startproject mysite

可以看到mysite文件夹,用命令行切换下mysite文件夹里面情况

.

├── manage.py

└── mysite

├── __init__.py

├── settings.py

├── urls.py

└── wsgi.py

1 directory, 5 files

而manage.py就是我们管理mysite文件夹管理命令文件,用sublime text 3打开该文件夹。

二,在site下建立app ,输入命令:

sudo python manage.py startapp spiderinfo

这个时候文件夹的情况如下:

2015-10-18 17:09:24 ☆ BruceUbuntu in ~/Desktop/djangoprojects/mysite

○ → tree

.

├── manage.py

├── mysite

│   ├── __init__.py

│   ├── __init__.pyc

│   ├── settings.py

│   ├── settings.pyc

│   ├── urls.py

│   └── wsgi.py

└── spiderinfo

├── admin.py

├── __init__.py

├── migrations

│   └── __init__.py

├── models.py

├── tests.py

└── views.py

三,app情况已经创建好了,django自带了ORM,我们只需要关注代码层的情况就可以了。

这个时候打开spiderinfo文件夹下的models.py,我们来简单的设计两张表

spider爬虫表,spiderconfig爬虫配置表

代码如下:

from django.db import models

# Create your models here.

class SpiderConfig(models.Model):

"""docstring for SpiderConfig"""

cid = models.AutoField(primary_key = True)

configname = models.CharField(max_length = 200)

createtime = models.DateTimeField()

class Spider(models.Model):

Sid = models.AutoField(primary_key = True)

SpiderName = models.CharField(max_length=200)

Config = models.ForeignKey(SpiderConfig,to_field='cid')

Enable = models.BooleanField(default = True)

每个Spider有一个SpiderConfig配置方案,这样就有一个主外键的关系,我们先将写好的关系同步到数据库:

python manage.py migrate

这个时候会自动产生脚本:

Operations to perform:

Synchronize unmigrated apps: staticfiles, messages

Apply all migrations: admin, contenttypes, auth, sessions

Synchronizing apps without migrations:

Creating tables...

Running deferred SQL...

Installing custom SQL...

Running migrations:

Rendering model states... DONE

Applying contenttypes.0001_initial... OK

Applying auth.0001_initial... OK

Applying admin.0001_initial... OK

Applying contenttypes.0002_remove_content_type_name... OK

Applying auth.0002_alter_permission_name_max_length... OK

Applying auth.0003_alter_user_email_max_length... OK

Applying auth.0004_alter_user_username_opts... OK

Applying auth.0005_alter_user_last_login_null... OK

Applying auth.0006_require_contenttypes_0002... OK

Applying sessions.0001_initial... OK

同步到数据库:

python manage.py syncdb

这个时候会产生同步的过程

○ → python manage.py syncdb

/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9

warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)

Operations to perform:

Synchronize unmigrated apps: staticfiles, messages

Apply all migrations: admin, contenttypes, auth, sessions

Synchronizing apps without migrations:

Creating tables...

Running deferred SQL...

Installing custom SQL...

Running migrations:

No migrations to apply.

You have installed Django's auth system, and don't have any superusers defined.

Would you like to create one now? (yes/no): yes

Username (leave blank to use 'bruce'):

Email address: nice_game@163.com

Password:

Password (again):

Superuser created successfully.

这个时候会让你输入管理界面的用户名密码,正常输入就可以了。

四,运行server,打开从网页中打开。

python manage.py runserver

打开server,输入网址:

http://127.0.0.1:8000/admin/

我们就可以在后台中看到管理界面了:

五,管理的后台看不到里面的内容,这个时候我们要编辑admin.py的内容,在后台管理界面来显示

代码:

from django.contrib import admin

import spiderinfo.models as app

# Register your models here.

class SpiderConfigAdmin(admin.ModelAdmin):

#要显示的字段列表

list_display = ['Cid','Configname','Createtime']

#要搜索的字段列表

search_fields = ['Configname','Createtime']

list_filter = ['Createtime']

#max show count

#list_max_show_all = 100

#Config_id

class SpiderAdmin(admin.ModelAdmin):

list_display =['SpiderName','Config','Enable']

#这里特别说明,比如我要根据外键的ConfigName来在Spider实体中的

search_fields = ['SpiderName','Config__Configname']

list_filter = ['Enable','SpiderName']

admin.site.register(app.Spider ,SpiderAdmin)

admin.site.register(app.SpiderConfig , SpiderConfigAdmin)

最的一步,在settings.py里面为我们的应用注册

效果如下:

============================end============================

总结:说30分钟,其实只是建立一个快速搭建的界面,django写的这么优雅和简洁,30分钟怎么可能了解全部呢,一个好的东西是需要花时间好好学习的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值