加上你的准备的时间,估计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分钟怎么可能了解全部呢,一个好的东西是需要花时间好好学习的。