django 多 APP urls的实现

Posted on 2021-08-16 17:18  baskbug  阅读(151)  评论(0编辑  收藏  举报

项目下urls.py

引入admin

from django.contrib import admin

 

引入path,include

from django.urls import path,include

引入项目下的views

from  . import views

==========================================

urlpatterns = [
    path('admin/', admin.site.urls),    #路由到admin管理页面

    path('index',views.Index),    #路由到index首页
    path('',views.Index),    #默认路由到index首页
]

 

 

 

APP下urls.py

引入path和app下的views(我这里是api.py)

from django.urls import path    #导入path
from proapp import api    #导入app下的views(我这里是api)

===============================================

urlpatterns = [
    path('test',api.test),
]

 

 

 项目下的settings.py文件

时区设置

LANGUAGE_CODE = 'en-us'

#TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False

redis配置

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "CONNECTION_POOL_KWARGS": {"max_connections": 100}
            # "PASSWORD": "123",
        }
    }
}

mysql配置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbname',  #数据库名称
        'HOST': 'localhost',
        'PORT': 3306,
        'USER': 'root',
        'PASSWORD': '123456',
    }
}

网页模版配置

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,"my_project/html_mod")],
        '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',
            ],
        },
    },
]

注册APP

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework_swagger',
    'rest_framework',
    '你创建的app',
]

 

 

 

---------------------------------

使用pymysql报错的,在项目和app路径下的__init__.py添加如下代码

import pymysql
pymysql.install_as_MySQLdb()