加上你的准備的時間,估計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分鍾怎么可能了解全部呢,一個好的東西是需要花時間好好學習的。
