[PYTHON] [Django] Let's try to clarify the part of Django that was somehow through in the test

Purpose

(1) Put out the mysterious variables in settings.py on the console and it will be fun. ② Hide password

OS etc.

Windows 10 Home

Environment

Click "Create new project" in the PyCharm title. 01.JPG

Give a title. This time, let's call it "django study". After giving a name, click "Create". 02.JPG

Open the settings screen by selecting "File"-> "Settings" (Ctrl + Alt + S) and select "Project Interpreter". 03.JPG

Click the "+" on the right. 04.JPG

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. image.png

Success if something increases. It also says installed successfully below. 06.JPG

There is a "terminal" at the bottom, so click on it. Open the terminal. image.png

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. image.png

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 image.png

Success if a rocket appears. The web server can stop the terminal with Ctrl + c. image.png

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. image.png

Contents of settings.py

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/'

Print the variables in settings.py and put them out on the terminal

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 image.png

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

[Django] Let's try to clarify the part of Django that was somehow through in the test
[Note] Let's try to predict the amount of electricity used! (Part 1)
How to find the coefficient of the trendline that passes through the vertices in Python
Try to extract the keywords that are popular in COTOHA
The story that the version of python 3.7.7 was not adapted to Heroku
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
Try to model the cumulative return of rollovers in futures trading
The meaning of ".object" in Django
Cython to try in the shortest
Heroku deployment of the first Django app that beginners are addicted to
Django ~ Let's display it in the browser ~
How to check the version of Django
Implement part of the process in C ++
Let's claim the possibility of pyenv-virtualenv in 2021
Try hitting the Spotify API in Django.
Feel free to write a test with nose (in the case of + gevent)
How to uniquely identify the source of access in the Django Generic Class View
How to count the number of elements in Django and output to a template
[AWS] Let's run a unit test of Lambda function in the local environment
Test the number of times you have thrown a query (sql) in django
Try to display the Fibonacci sequence in various languages in the name of algorithm practice
Correspondence of the event that the result of form.is_valid () is always False in Django2 system
Try to display the railway data of national land numerical information in 3D
[Verification] Try to align the point cloud with the optimization function of pytorch Part 1
The _authenticate_with_backend function was obsolete in django auth.autenticate
Try installing only the core part of Ubuntu
The inaccuracy of Tensorflow was due to log (0)
The story of viewing media files in Django
Try to simulate the movement of the solar system
Let's erase the unintended (base) that appears in the terminal [Get out of the Conda environment]
Here is one of the apps with "artificial intelligence" that I was interested in.
Note that the method of publishing modules to PyPI has changed in various ways.
The wall of changing the Django service from Python 2.7 to Python 3
How to get the number of digits in Python
Try to solve the problems / problems of "Matrix Programmer" (Chapter 1)
Try to visualize the room with Raspberry Pi, part 1
I want to write in Python! (2) Let's write a test
Try to estimate the number of likes on Twitter
Set the form DateField to type = date in Django
Find the part that is 575 from Wikipedia in Python
Try to get the contents of Word with Golang
Let's use the open data of "Mamebus" in Python
Let's test the medical collapse hypothesis of the new coronavirus
I tried to erase the negative part of Meros
The story that the return value of tape.gradient () was None
Try to decipher the login data stored in Firefox
Modules that may go through the shell in Python
The story that Japanese output was confused with Django
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
[Python] Let's change the URL of the Django administrator site
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
Let's get notified of the weather in your favorite area from yahoo weather on LINE! ~ PART2 ~
A story that makes it easier to see Model debugging in the Django + SQLAlchemy environment
Let's statically check and format the code of E2E automatic test written in Python [VS Code]
What seems to be a template of the standard input part of the competition pro in python3
I was in charge of maintaining the Fabric script, but I don't know.> <To those who
Let's introduce the library currently used by engineers with about 3 years of experience in Django
Try to visualize the nutrients of corn flakes that M-1 champion Milkboy said with Python