(1) Put out the mysterious variables in settings.py on the console and it will be fun. ② Hide password
Windows 10 Home
Click "Create new project" in the PyCharm title.
Give a title. This time, let's call it "django study". After giving a name, click "Create".
Open the settings screen by selecting "File"-> "Settings" (Ctrl + Alt + S) and select "Project Interpreter".
Click the "+" on the right.
A list of available packages will appear. Enter django in the search bar and select the one that appears. Press "Install Package" and wait for a while.
Success if something increases. It also says installed successfully below.
There is a "terminal" at the bottom, so click on it. Open the terminal.
Enter in the terminal.
console
# "Verification"Create a project named.
(venv) C:\*****\*****\PycharmProjects\djangostudy> django-admin startproject Verification
A folder called Verification has been created.
Enter the following in the terminal.
console
#In the Verification folder"manage.py"I have a need to move.
(venv) C:\*****\*****\PycharmProjects\djangostudy> cd Verification
#Start the web server and run the Verification program.
(venv) C:\*****\*****\PycharmProjects\djangostudy\Verification> python manage.py runserver
The terminal display looks like this, so click the url that is blue. http://127.0.0.1:8000
Success if a rocket appears. The web server can stop the terminal with Ctrl + c.
Enter the following in the terminal.
console
# "testapp"Create an application named.
(venv) C:\ ~ \Verification> python manage.py startapp testapp
A folder called "testapp" has been created.
The contents of Verification / settings.py. I don't know what it is.
settings.py
"""
Django settings for Verification project.
Generated by 'django-admin startproject' using Django 3.1.2.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# 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!
#Since security is written above, I will hide it.
SECRET_KEY = 'hogehogehogehogehogehogehogehogehogehogehogehogeho'
# 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',
]
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 = 'Verification.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'Verification.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/'
settings.py BASE_DIR
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
I don't know what it is, I try to display BASE_DIR
Add the following functions to settings.py
settings.py
def verifyConst(varname, var):
#variable:Display the contents of the variable on the terminal.
varname += ' : '
print(varname + str(var))
# BASE_Contents of DIR
verifyConst('__file__', __file__)
verifyConst('Path(__file__)', Path(__file__))
verifyConst('Path(__file__).resolve()', Path(__file__).resolve())
verifyConst('Path(__file__).resolve().parent', Path(__file__).resolve().parent)
verifyConst('BASE_DIR', BASE_DIR)
print()
result
BASE_DIR = Path(file).resolve().parent.parent It seems that BASE_DIR is the place where the manage.py file is placed, and it seems that various things are done to find it. Path (file) .resolve (): Path of settings.py Path (file) .resolve (). parent: parent folder of settings.py Path (file) .resolve (). parent.parent: parent folder of settings.py parent folder
.parent seems to represent the parent folder.
Recommended Posts