2. locallibrary website app
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website
default structure new project
- manage.py : script used to create applications, work with databases, and start the development web server.
- subfolder with same name as parent folder (parent = project , subfolder = app )
with :
- init.py is an empty file that instructs Python to treat this directory as a Python package.
- settings.py contains all the website settings, including registering any applications we create, the location of our static files, database configuration details, etc.
- urls.py defines the site URL-to-view mappings.
While this could contain all the URL mapping code, it is more common to delegate some of the mappings to particular applications, as you’ll see later.
- wsgi.py is used to help your Django application communicate with the web server. You can treat this as boilerplate.
- asgi.py is a standard for Python asynchronous web apps and servers to communicate with each other. Asynchronous Server Gateway Interface (ASGI) is the asynchronous successor to Web Server Gateway Interface (WSGI). ASGI provides a standard for both asynchronous and synchronous Python apps, whereas WSGI provided a standard for synchronous apps only. ASGI is backward-compatible with WSGI and supports multiple servers and application frameworks.
cd /mnt/data/dev/django
django-admin startproject locallibrary
cd locallibrary
# create a second app catalog = a standard data framework /modeling app in django
python3 manage.py startapp catalog
# modify the project settings file , to registeri the catalog app with the project :
xed /mnt/data/dev/django/locallibrary/locallibrary/settings.py
add
# Add our new application
'catalog.apps.CatalogConfig', #This object was created for us in /catalog/apps.py
as last line to the section INSTALLED_APPS = [
# We'll use the default SQLite database , no extra work
# if another db is used , must be configured in this file
# change timezone :
TIME_ZONE = 'Europe/Brussels'
add next python code block
to the end of the urls.py file : (verify with tutorial)
# Use include() to add paths from the catalog application
from django.urls import include
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)
create a new urls.py file in /mnt/data/dev/django/locallibrary/catalog/ :
# Use include() to add paths from the catalog application
from django.urls import include
urlpatterns += [
path('catalog/', include('catalog.urls')),
]
define the tables for the models in the database
python3 manage.py makemigrations
python3 manage.py migrate
start in a terminal the server :
python3 manage.py runserver
now test run http://127.0.0.1:8000/ in a browser , it should redirect to http://127.0.0.1:8000/catalog/
with error 404 , which is normal at this point