Python学习---Django的新工程设置模板

Posted 小a玖拾柒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python学习---Django的新工程设置模板相关的知识,希望对你有一定的参考价值。

该模板完全可以在创建好新工程后进行部分代码替换

创建app01的  python startapp app01  

创建static子目录

settings.py

"""
Django settings for user_manager project.
Generated by ‘django-admin startproject‘ using Django 2.0.1.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""
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__)))

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ‘(mtzmh6dqyi=f4x6&hb15!gznpuxqm2-2t=xeq$3ga=120qlm&‘

# 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‘,
    ‘app01‘,   # 注册app
]

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 = ‘user_manager.urls‘

TEMPLATES = [
    {
        ‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘,
        # ‘DIRS‘: [],
        ‘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‘,
            ],
        },
    },
]

WSGI_APPLICATION = ‘user_manager.wsgi.application‘


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

DATABASES = {
    ‘default‘: {
        ‘ENGINE‘: ‘django.db.backends.sqlite3‘,
        ‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘),
    }
}
# Password validation
# https://docs.djangoproject.com/en/2.0/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/2.0/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/2.0/howto/static-files/

STATIC_URL = ‘/static/‘

TEMPLATE_DIRS = (os.path.join(BASE_DIR,  ‘templates‘),)

STATICFILES_DIRS = (os.path.join(BASE_DIR, "statics"),)     # 现添加的配置,这里是元组,注意逗号

# 配置session
SESSION_ENGINE = ‘django.contrib.sessions.backends.db‘  # 引擎(默认)

SESSION_COOKIE_NAME = "sessionid"  # Session的cookie保存在浏览器上时的key,即:sessionid=随机字符串(默认)
SESSION_COOKIE_PATH = "/"       # Session的cookie保存的路径(默认)
SESSION_COOKIE_DOMAIN = None    # Session的cookie保存的域名(默认)
SESSION_COOKIE_SECURE = False   # 是否Https传输cookie(默认)
SESSION_COOKIE_HTTPONLY = True  # 是否Session的cookie只支持http传输(默认)
SESSION_COOKIE_AGE = 1209600    # Session的cookie失效日期(2周)(默认)
SESSION_EXPIRE_AT_BROWSER_CLOSE = False  # 是否关闭浏览器使得Session过期(默认)
SESSION_SAVE_EVERY_REQUEST = False       # 是否每次请求都保存Session,默认修改之后才保存(默认)

view.py

from django.shortcuts import render, redirect, HttpResponse
from app01 import models

url.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from app01 import views

初始化数据库

python manage.py makemigrations
python manage.py migrate

以上是关于Python学习---Django的新工程设置模板的主要内容,如果未能解决你的问题,请参考以下文章

Python之路58-Django安装配置及一些基础知识点

Python学习---django模板继承180123

Django入门笔记

[python][django学习篇][10]再次修改博客首页模板

Python大神 - Django(深层学习)-- 模板语言

干货福利:如何使用Python中Django模板?