一 、建立一个普通的polls项目,如官网,注意两点
修改完models后,需要
1.python manage.py migrate
2.增加url
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
- 修改settings
INSTALLED_APPS = [
'simpleui',
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
- 修改项目下的 admin.py
from .models import Question
admin.site.register(Question)
5.运行
python manage.py runserver
二、通过已知数据库建项目
1.新增库
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djwork',
'USER': 'users',
'PASSWORD': '123456',
'HOST': '192.168.4.2',
'PORT': '3306',
},
'djapp': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djapp',
'USER': 'users',
'PASSWORD': '123456',
'HOST': '192.168.4.2',
'PORT': '3306',
},
DATABASE_ROUTERS = ['trf.database_router.DatabaseAppsRouter']
并根目录建立文件trf/database_router.js
新建类 DatabaseAppsRouter
DATABASES_APPS_MAPPING = {
'polls': 'default',
'djapp': 'djapp',
}
class DatabaseAppsRouter():
def db_for_read(self, model, **hints):
if model._meta.app_label in DATABASES_APPS_MAPPING:
return DATABASES_APPS_MAPPING[model._meta.app_label]
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in DATABASES_APPS_MAPPING:
return DATABASES_APPS_MAPPING[model._meta.app_label]
return None
- 同上建一个models.py文件
注意下方加入 Meta:
class Meta:
managed = False
db_table = 'testdb'
3.python manage.py inspectdb
python manage.py makemigreations
python manage.py migrate djapp
完成,后面一切同polls
官网说的太少了,根本不够应用,加上这些勉强应用
需要一个简版的说明,把常用的功能都加上