Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Django development configuration

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The company's business provides API data interface calls, but developers do not have time to develop data statistics, so they write a python scheduled task script to do statistics every day and send statistical results to the relevant personnel. Manual statistics are needed to solve temporary problems, to view historical calls and precise point-in-time searches. So I want to write a set of procedures for page statistics and display, which can be looked up and used by relevant personnel.

Environment Centos6.5+Python2.7.12+Django8.1; (Note: there is a big difference between future versions and versions of Django8.1)

Writing this article is not only to share with you, but also to find convenience for yourself in the future.

The environment is installed slightly.

# Create New Project "Consume"

~] django-admin.py startproject consume

~] cd consume

We need to make statistics on the data calls of the two business projects, and then create two business APP;. The entire code logic is completed in the main project consume, and the two business projects mainly provide database support.

# Create professional work app

~]. / manage.py startapp work1

~]. / manage.py startapp work2

From top to next, my configuration is marked red, and other configurations are not explained:

# Setup master configuration file

# Setup access permissions

ALLOWED_HOSTS = ["*"] / / the project is placed on the intranet, so all access is allowed for internal applications.

# 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', / / main project

'work1', / / Business items that provide data calls

'work2', / / Business items that provide data calls

]

# Setup the path for template files

TEMPLATES = [

{

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

'DIRS': [

Os.path.join (BASE_DIR, 'templates'), / / is empty by default. Plan the directory structure according to your needs.

/ / the storage path of the configuration template file is at the same level as the project

]

'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 (two business projects use different databases, here is a multi-database configuration)

DATABASES = {

'default': {/ / may not have a default database, but the 'default' configuration must exist

/ / I need users to log in here, using a separate database, locally

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

'NAME':'*

'HOST': 'localhost'

'USER':'*

'PORT': '3306'

'PASSWORD':'*

}

/ / the following two databases are for business projects and are not local

'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') / / here the static file storage path is at the same level as the template file

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')

]

/ / retrieve static file engine configuration

STATICFILES_FINDERS = [

'django.contrib.staticfiles.finders.FileSystemFinder'

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

]

For the use of multiple libraries, please refer to the official document http://python.usyiyi.cn/translate/django_182/topics/db/multi-db.html.

# Multi database operation

Synchronize data:

. / manage.py makemigrations / / generate synchronization data files and store them in the migrations directory of the application

. / manage.py migrate-- database work1 / / synchronize data

Generate models.py:./manage.py inspectdb from the database in reverse-- database work1 > consume/work1/models.py

Lookup data: models.work1.objects.using ('work1'). All ()

/ / for multi-database operations, you must specify the database for operation, and do not specify the default default library. This is a simple way to use multi-databases, and / / you can configure database routing for use. Please refer to the official documentation.

# 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 / / the version I use is 2.0.14; configuration file / etc/uwsgid.ini

# configure the application

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

~] cat > > consume/apps.pyconsume/work2/models.py

# write consume/models.py and design user login authentication class

~] cat > > consume/models.py

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report