django開發配置

    公司業務提供API數據接口調用,但開發沒時間開發數據統計,寫了一個python定時任務腳本每天做統計併發送統計結果於相關人員。解決了臨時問題,查看歷史調用量和精確時間點查尋等還需要人工統計。於是就想寫一套頁面統計展示的程序,給相關人員查尋使用。

    環境Centos6.5+Python2.7.12+Django8.1;(注:Django8.1以後的版本與之間的版本差異比較大)

寫這篇文章一是與大家一起分享,也是爲以後自己查尋方便。

環境安裝略......


# Create New Project "Consume"

~] django-admin.py startproject consume

~] cd consume


我們需要對兩個業務項目的數據調用做統計,再創建兩個業務APP;整個代碼邏輯都在主項目consume中完成,兩個業務項目主要提供數據庫支持。


# Create professional work app

~] ./manage.py startapp work1

~] ./manage.py startapp work2


從上至下一項一項配置,我的配置標紅,其它配置不做解釋:

# Setup master configuration file

    # Setup access permissions

    ALLOWED_HOSTS = ["*"]            // 項目放在內網,內部應用所以允許所有訪問

    # Add projessional work project application

    INSTALLED_APPS = [

        'django.contrib.admin',

        'django.contrib.auth',

        'django.contrib.contenttypes',

        'django.contrib.sessions',

        'django.contrib.messages',

        'django.contrib.staticfiles',

        'consume',                                // 主項目

        'work1',                                     // 提供數據調用的業務項目

        'work2',                                     // 提供數據調用的業務項目

    ]

    # Setup the path for template files

    TEMPLATES = [

        {

            'BACKEND': 'django.template.backends.django.DjangoTemplates',

            'DIRS': [

                os.path.join(BASE_DIR, 'templates'),     // 默認是空的,根據自己需要規劃目錄結構

                                                                               // 配置模板文件存放路徑與項目同級

            ],

            'APP_DIRS': True,

            'OPTIONS': {

                'context_processors': [

                    'django.template.context_processors.debug',

                    'django.template.context_processors.request',

                    'django.contrib.auth.context_processors.auth',

                    'django.contrib.messages.context_processors.messages',

                ],

            },

        },

    ]

    #Setup database (兩個業務項目用的是不同的數據庫,這裏是多庫配置)

    DATABASES = {

        'default': {               // 可以沒有默認的數據庫,但'default'配置必須存在

                                      // 我這裏需要用戶登陸,使用單獨的數據庫,在本地

            'ENGINE': 'django.db.backends.mysql',

            'NAME': '****',

            'HOST': 'localhost',

            'USER': '****',

            'PORT': '3306',

            'PASSWORD': '****',

        },

        // 下面兩個是業務項目的數據庫,不在本地

        'work1': {

            'ENGINE': 'django.db.backends.mysql',

            'NAME': '****',

            'HOST': '****',

            'USER': '****',

            'PORT': '3307',

            'PASSWORD': '****',

        },

        'work2': {

            'ENGINE': 'django.db.backends.mysql',

            'NAME': '****',

            'HOST': '****',

            'USER': '****',

            'PORT': '3307',

            'PASSWORD': '****',

        }

    }

    # Setup the path for static files

    STATIC_URL = '/static/'                                              // Model: http://model.domain.com/static/

    STATIC_ROOT = os.path.join(BASE_DIR, 'static')    // 這裏配置靜態文件存放路徑與模板文件同級

    STATICFILES_DIRS = [

        os.path.join(STATIC_ROOT, 'css'),

        os.path.join(STATIC_ROOT, 'js'),

        os.path.join(STATIC_ROOT, 'fonts'),

        os.path.join(STATIC_ROOT, 'img'),

    ]

    // 檢索靜態文件引擎配置

    STATICFILES_FINDERS = [

        'django.contrib.staticfiles.finders.FileSystemFinder',

        # 'django.contrib.staticfiles.finders.AppDirctoriesFinder',

    ]

多庫的使用可以參考官方文檔http://python.usyiyi.cn/translate/django_182/topics/db/multi-db.html。

# Multi database operation

同步數據

    ./manage.py makemigrations                   // 生成同步數據文件,存放在應用的migrations目錄下

    ./manage.py migrate --database work1   // 同步數據

從數據庫反向生成models.py:./manage.py inspectdb --database work1 >consume/work1/models.py

查尋數據:models.work1.objects.using('work1').all()

    // 多數據庫操作必須指定操作的數據庫,不指定默認爲default庫;這是一種簡單的多庫使用方法,也                                 // 可以配置數據庫路由使用,可參考官方文檔。

# Create the path for tamplates files and static files and code

~] pwd

/path/consume

~] mkdir templates static consume/script

~] tree -d

├── work1                   // professional work1

│   └── migrations

├── consume              // master project

│   ├── migrations

│   └── script             // the code for the program

├── work2                  // professional work2

│   └── migrations

├── logs                     // uwsgi  log

├── static                   // static file; using bootstrap; reference:http://www.bootcss.com/

│   ├── css

│   ├── fonts

│   ├── img

│   └── js

├── templates           // template files

└── tmp                    // temporary file and uwsgi pidfile

# Installaion uWSGI

~] pip install uWSGI      // 我使用的版本是2.0.14; 配置文件/etc/uwsgid.ini


# 配置應用

~] touch consume/{models.py,views.py}

~] cat >>consume/apps.py<<EOF              // 默認沒有

from __future__ import unicode_literals

from django.apps import AppConfig


class AntiFraudConfig(AppConfig):

    name = 'consume'

EOF


# 反向生成數據models.py文件

~] ./manage.py inspectdb --database work1 >consume/work1/models.py

~] ./manage.py inspectdb --database work2 >consume/work2/models.py

# 編寫consume/models.py,設計用戶登陸認證class

~] cat >>consume/models.py<<EOF

from __future__ import unicode_literals

from django.db import models

# from datetime import datetime

class User(models.Model):

    username = models.CharField(max_length=30, unique=True)

    password = models.CharField(max_length=50)

    createtime = models.DateTimeField(auto_now_add=True)

    updatetime = models.DateTimeField(auto_now=True)

    def __unicode__(self):

        return self.username

EOF

# 創建數據庫並配置訪問權限;略......

# 同步到數據庫

~] ./manage.py makemigrations

~] ./manage.py migrate                                // 這裏沒加--database參數,默認同步default庫


# 配置consume/urls.py

from django.conf.urls import url, include

# from django.contrib import admin

from . import views

from django.conf import settings

from django.conf.urls.static import static      // Django-10配置靜態文件生效

urlpatterns = [

    # url(r'^admin/', admin.site.urls),

    url(r'^$', views.index, name="Consume"),

    url(r'^login/', views.login, name="sign"),

    url(r'^logout/$', views.logout, name="logout"),

    url(r'^currentweek/$', views.currentWeek, name='CW'),

    url(r'^currentmonth/$', views.currentMonth, name='CM'),

    url(r'^lastweek/$', views.lastWeek, name='LW'),

    url(r'^lastmonth/$', views.lastMonth, name='LM'),

    url(r'^score/$', views.creditScore, name='S'),

    url(r'^score/(\S+)/', views.scoreClient, name='SC'),

    url(r'^anti/$', views.antiFraud, name='A'),

    url(r'^anti/(\S+)/', views.antiClient, name='AC'),

    url(r'^antifraud', include('anti_fraud.urls')),

    url(r'^creditscore', include('credit_score.urls')),

] + static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)    // 必須加上這行配置                                                                                                                                       // settings.py中的靜態文件                                                                                                                                                     // 配置纔會生效


# 下載bootstrap到static並解壓;略......

開媽寫代碼.............

主頁上可以做5個標籤頁上,常用固定時間段的數據調用查尋;Dashboard後面摭住的三個button是           兩個項目和登陸用戶。

主頁效果如下圖:

 

進入項目可以做詳細查詢,如下圖:


詳細查詢頁面效果如下圖:

可根據時間段要求,針對某一個用戶做詳細查詢,不再上圖

OVER

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章