30分鐘快速搭建Web CRUD的管理平臺--django神奇魔法

    加上你的準備的時間,估計30分鐘徹底夠用了,由於最近在作爬蟲管理平臺,想着快速開發,沒想到python web平臺下有這麼很是方便的框架,簡潔而優雅。將本身的一些坑總結出來,方便給你們的使用。python

 

準備環境:web

系統:win7 or ubuntu 數據庫

django版本:1.8.5django

python版本:2.7.6ubuntu

數據庫:自帶的SQLLITE3bash

IDE: sublime text 3session

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

一,選擇文件夾,用命令行建立文件夾框架

 

sudo django-admin startproject mysite

 

 

能夠看到mysite文件夾,用命令行切換下mysite文件夾裏面狀況python2.7

.
├── 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分鐘怎麼可能瞭解所有呢,一個好的東西是須要花時間好好學習的。

相關文章
相關標籤/搜索