1. Creating a skeleton website - LiVanych/locallibrary GitHub Wiki
Development Envinronment
python -V
Python 3.6.5
python -m django --version
2.1.2
Creating the project
django-admin startproject locallibrary
cd locallibrary
Creating the catalog application
./manage.py startapp catalog
├── catalog
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── locallibrary
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ └── settings.cpython-36.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
Registering the catalog application
vim locallibrary/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'catalog.apps.CatalogConfig',
]
Specifying the database
it makes sense to use the same database for development and production where possible, in order to avoid minor differences in behaviour. You can find out about the different options in Databases (Django docs).
cat locallibrary/settings.py
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
...
Because we are using SQLite, we don't need to do any further setup here. Let's move on!
Other project settings
TIME_ZONE — this should be made equal to a string from the standard [[https://en.wikipedia.org/wiki/List_of_tz_database_time_zones|List of tz database time zones]] (the TZ column in the table contains the values you want). Change your TIME_ZONE value to one of these strings appropriate for your time zone, for example: TIME_ZONE = 'Europe/London' There are two other settings you won't change now, but that you should be aware of:
SECRET_KEY. This is a secret key that is used as part of Django's website security strategy. If you're not protecting this code in development, you'll need to use a different code (perhaps read from an environment variable or file) when putting it into production.
DEBUG. This enables debugging logs to be displayed on error, rather than HTTP status code responses. This should be set to False on production as debug information is useful for attackers, but for now we can keep it set to True.
Hooking up the URL mapper
vim locallibrary/urls.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
# Use include() to add paths from the catalog application
from django.urls import include
from django.urls import path
urlpatterns += [
path('catalog/', include('catalog.urls')),
]
#Add URL maps to redirect the base URL to our application
from django.views.generic import RedirectView
urlpatterns += [
path('', RedirectView.as_view(url='/catalog/', permanent=True)),
]
# Use static() to add url mapping to serve static files during development (only)
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
vim catalog/urls.py
from django.urls import path
from . import views
urlpatterns = [
]
Running database migrations
python manage.py makemigrations
python manage.py migrate
Testing the website framework
python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
August 15, 2018 - 16:11:26
Django version 2.1, using settings 'locallibrary.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
See also
Writing your first Django app - part 1 (Django docs).
Applications (Django Docs). Contains information on configuring applications.
Full version of article read here (MDN Site)