[Personal notes] Python, Django

Memo

Project creation

$django-admin startproject project name

When you create a project, the same directory as the project name is created. Files used throughout the project are saved here.

__inti__.py

A script file that does the initialization when you run your Django project. asgi.py A program for creating asynchronous web applications called ASGI settings.py Describe the project setting information file, add the application name to INSTALLED_APPS [] when creating the application urls.py Manage URLs in your project wsgi.py WSGI, a popular web application program

Application creation

$python manage.py startapp application name

== Configuration when creating application folder == migrations folder

Files of database related functions are collected __inti__.py File for application initialization process admin.py Files for admin tools apps.py Summarize the processing of the application itself models.py Model processing tests.py Program testing views.py Screen display

== Individually created items == urls.py

Because you need to configure routing for each application templates folder Use the template function (Create a folder with the application name in the templates folder. Link with the application added to settings.py, go to render with the render function) static folder Use static files, create a folder with the application name in the static folder, and use the static template tag to automatically look inside the static folder

Web server start

$python manage.py runserver

Creation of migration file

$python manage.py makemigrations application name

Migration execution

$python manage.py migrate

Connect to an existing DB

・ Reference URL for connecting to existing Django DB

This time it was necessary to connect to ClearDB (mysql) in the heroku environment, so record it.

settings.py


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

        #herokuDB connection information
        'ENGINE': 'django.db.backends.mysql',
        'HOST': os.getenv('DB_HOSTNAME'),
        'NAME': os.getenv('DB_NAME'),
        'USER': os.getenv('DB_USERNAME'),
        'PASSWORD': os.getenv('DB_PASSWORD'),
    }
}

■ Connection test OK if you can connect to the DB of the configuration file with the following command

$python manage.py dbshell

■ Confirmation of connection information to herokuDB After logging in to heroku, execute the following command Since environment variables can be listed, the connection information to the DB is in CLEARDB_DATABASE_URL.

$heroku config --app application name

Model creation from existing DB (automatic generation)

Once the connection test is complete, create a model for use with django.

$python manage.py inspectdb

When you type this command, it will be based on the DB information. The model code is returned. (In this case, it is not saved.)

If the code of the returned model seems to be okay, copy and paste it into the model. Alternatively, a model file is generated by typing the following command.

$python manage.py inspectdb >> models.Path to py/models.py

Recommended Posts

[Personal notes] Python, Django
python personal notes
missingintegers python personal notes
django notes
Django notes
Personal notes for python image processing
Python Pandas Data Preprocessing Personal Notes
(Personal notes) Python metaclasses and metaprogramming
Python study notes _000
Python learning notes
Python Django Tutorial (5)
Python Django Tutorial (2)
[Django] as_view () notes
Python beginner notes
Python study notes_006
Django Template notes
Python Django Tutorial (8)
python C ++ notes
Python Django Tutorial (6)
Python study notes _005
Python grammar notes
Python Library notes
Python personal Q.A
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
python pandas notes
[Django] JWT notes
Python study notes_001
python learning notes
Python Django Tutorial (3)
Python3.4 installation notes
Python Django Tutorial (4)
WEB scraping with Python (for personal notes)
Django 1.11 started with Python3.6
Python Django tutorial summary
Django python web framework
Pandas Personal Notes Summary
Python package development notes
Celery notes on Django
Django Python shift table
Personal notes to doc Python code in Sphinx
python decorator usage notes
Python ipaddress package notes
Python Pickle format notes
[Python] pytest-mock Usage notes
First Python miscellaneous notes
Matlab => Python migration notes
Notes around Python3 assignments
Notes using Python subprocesses
Python Django CSS reflected
Python try / except notes
Python framework bottle notes
Python notes using perl-ternary operator
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Python3 programming functions personal summary
O'Reilly python3 Primer Learning Notes
Python standard unittest usage notes
Python notes to forget soon
Do Django with CodeStar (Python3.8, Django2.1.15)
python * args, ** kwargs Usage notes