Creating an App
Part of this chapter is based on tutorials by Django Girls Tutorial : https://tutorial.djangogirls.org
To keep everything tidy, we will create a separate application inside our project. It is very nice to have everything organized from the very beginning. To create an application we need to run the following command in the console (from textbased
directory where manage.py
file is):
Mac OS X and Linux:
(myvenv) ~/textbasedgame$ python manage.py startapp myfirstgame
Windows:
(myvenv) C:\Users\Name\textbasedgame> python manage.py startapp myfirstgame
You will notice that a new myfirstgame
directory is created and it contains a number of files now. The directories and files in our project should look like this:
textbasedgame
├── myfirstgame
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── manage.py
└── mysite
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
After creating an application, we also need to tell Django that it should use it.
We do that in the file tbgsite/settings.py
. We need to find INSTALLED_APPS
and add a line containing myfirstgame
just above ]
. So the final product should look like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myfirstgame',
]