Djangoを始めて必要な設定やコマンドなどの備忘録
##プロジェクト作成
django-admin startproject [project-name] .
##settingsの変更
[project-name]/settings.py
TIME_ZONE = 'Asia/Tokyo'
LANGUAGE_CODE = 'ja'
#静的ファイルを公開する場合
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
#公開ホストの追加
ALLOWED_HOSTS = ['127.0.0.1', [host]]
## 例
ALLOWED_HOSTS = ['127.0.0.1', '.pythonanywhere.com']
データべース作成
python manage.py migrate
Webサーバー起動
python manage.py runserver [host]:[port]
新しいアプリケーションの作成
python manage.py startapp [app-name]
設定に新しいアプリケーションを追加
[project-name]/settings.py
INSTALLED_APPS = [
...
'blog.apps.BlogConfig', # 追加したアプリのAppクラス名
]
Modelからテーブルを作成するためのマイグレーション作成
python manage.py makemigrations [app-name]
マイグレーション実行
python manage.py migrate [app-name]
管理画面へのアクセス
http://[host]:[port]/admin
Superユーザーの作成
python manage.py createsuperuser
.gitignore
ベースは、https://www.gitignore.io を使用
.gitignore
# Created by https://www.gitignore.io/api/django
# Edit at https://www.gitignore.io/?templates=django
### Django ###
*.log
*.pot
*.pyc
__pycache__/
local_settings.py
db.sqlite3
db.sqlite3-journal
media
# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/
# in your Git repository. Update and uncomment the following line accordingly.
# <django-project-name>/staticfiles/
### Django.Python Stack ###
# Byte-compiled / optimized / DLL files
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# Mr Developer
.mr.developer.cfg
.project
.pydevproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
## ここからカスタマイズ
# My pipenv
myvenv/
#VS code
.vscode/
## ここまでカスタマイズ
# End of https://www.gitignore.io/api/django