A project should be the name for the collection of “apps” that will be running in your project. If a project is a website that encompasses many different functionalities (blog, payment portal, chat, photo site, etc), then each function could be considered an app within the project.
let’s say you want your project to be your personal website
django-admin startproject personal-website
cd personal-website
You can run the project right away, (though there won’t be much to do)
python3 manage.py runserver
hit ctrl+c to stop
open personal-website/settings.py in your favorite text editor, for example:
nano personal-website/settings.py
Edit the time zone. Comment out:
TIME_ZONE = 'UTC'
And replace it with
TIME_ZONE = 'America/Phoenix'
to find a list of valid time zones, run python in the terminal;
python
in the python interpreter type the following lines
import pytz
pytz.country_timezones('US')
this will return:
['America/New_York', 'America/Detroit', 'America/Kentucky/Louisville', 'America/Kentucky/Monticello', 'America/Indiana/Indianapolis', 'America/Indiana/Vincennes', 'America/Indiana/Winamac', 'America/Indiana/Marengo', 'America/Indiana/Petersburg', 'America/Indiana/Vevay', 'America/Chicago', 'America/Indiana/Tell_City', 'America/Indiana/Knox', 'America/Menominee', 'America/North_Dakota/Center', 'America/North_Dakota/New_Salem', 'America/North_Dakota/Beulah', 'America/Denver', 'America/Boise', 'America/Phoenix', 'America/Los_Angeles', 'America/Anchorage', 'America/Juneau', 'America/Sitka', 'America/Metlakatla', 'America/Yakutat', 'America/Nome', 'America/Adak', 'Pacific/Honolulu']
if you want to find the timezones for a different country, you can replace 'US' with your country code:
pytz.country_names.keys()
Navigate in your terminal to the personal-website folder if you are not already there
Create a new blog app:
python3 manage.py startapp blog
You may want to pre-define a custom model before initializing your database, according to this suggestion: https://docs.djangoproject.com/en/4.0/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project
Add your new app to the bottom of the list of installed apps. We will revisit this later when we want to add plugins, other existing functionality, or another custom app:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog.apps.BlogConfig'
]
navigate to personal-website/personal-website/urls.py Add this code to the bottom (you can clean it up later)
from django.urls import include
urlpatterns += [
path('blog/', include('blog.urls')),
]
This will map any url starting with blog/ to the blog app. Later, we will revisit this when we want to expand our blog’s URL reach, but for now, we can leave it here
Also add a standard way for django to deal with static files. To the same file, add:
# 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)
Finally, add url mappings to your new app as well. In personal-website/blog/urls.py, add:
from django.urls import path
from . import views
urlpatterns = [
]
This is empty now but we will fill it with the specifics of the blog app.
python3 manage.py makemigrations
python3 manage.py migrate
You will use the next command to create tables for your app if they do not already exist, or if you have deleted the database and are starting it over from scratch.
python3 manage.py migrate --run-syncdb
and finally you need to create at least one user who can access the admin site with full permissions:
python manage.py createsuperuser
Finally, to run the website for the first time, execute the following in your terminal:
python manage.py runserver
if you want to specify a specific ip address or port, you can expand on the command:
python3 manage.py runserver 0.0.0.0:80