如何在Django 3.1中添加“模板”和“静态”文件夹

问题描述

setting.py

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

#TEMPLATES_DIR = path.join(BASE_DIR,'templates')    #it dosen't work
#STATICS_DIR = path.join(BASE_DIR,'statics')             #it also dosen't work

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# Security WARNING: keep the secret key used in production secret!
SECRET_KEY = '0#nf@$8nm5jb0)meuq&j6kztt534@nhk)k&$zh=#emcxi7_7fo'

# Security WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application deFinition

INSTALLED_APPS = [
    'django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','musicianapp',]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware',]

ROOT_URLconf = 'musicianList.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates','Dirs': [TEMPLATES_DIR,],'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',},]

Wsgi_APPLICATION = 'musicianList.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3','NAME': BASE_DIR / 'db.sqlite3',}
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',{
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',{
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',{
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS,JavaScript,Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_Dirs = [
    STATICS_DIR,]

请注意顶部第三顶部行,这与 Djago2.7版本

的主要区别

在Djnago2.7中,按如下所示导入了操作系统,

import os

# Build paths inside the project like this: os.path.join(BASE_DIR,...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

并且我使用以下代码连接模板和static forlder

TEMPLATES_DIR =os.path.join(BASE_DIR,"templates")
STATICS_DIR =os.path.join(BASE_DIR,"statics")

但是下面给出的 Djnago 3.1 from pathlib imported Path格式存在差异,

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

并且os技术无法正常工作

TEMPLATES_DIR =os.path.join(BASE_DIR,"statics")

如何与模板和statics文件夹建立连接,需要帮助。

注意:我在已经显示 manage.py db.sqlite3 文件的主要项目中创建模板和静态文件夹。在我的项目方向的屏幕截图下方,

enter image description here

解决方法

如果您查看 <EditText android:id="@+id/date_time_edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/add_date_time" android:importantForAutofill="no" android:ems="10" android:clickable="true" android:focusable="false" android:editable="false" android:onClick="@{() -> viewModel.showDateTimeDialog()}" /> 文件的顶部,则会看到settings.py正在使用BASE_DIR

pathlib

因此,您不再需要BASE_DIR = Path(__file__).resolve().parent.parent 来加入路径,只需执行以下操作即可:

os

这是3.1中的新功能-您可以在此处的发行说明中了解它:https://docs.djangoproject.com/en/3.1/releases/3.1/