Here's what you need to know to get started with Django.
To create a new Django project, change to the directory for your new project and run the following command:
django-admin startproject project name
If you want to reduce the created folder hierarchy by one, add .
(Half-width space and period) after the project name.
I often execute the following command in the directory with the project name.
django-admin startproject config .
In the project directory to be created, a file that describes the settings related to all the applications to be created will be created, so set the directory name to config
and place it side by side with the application directory for the project directory (create it in advance). It is placed in the folder where it was.
To create the application, run the following command.
python manage.py startapp application name
Create a directory for saving CSS and javascript files and images in the same hierarchy as manage.py
.
I often set the directory for saving CSS files and javascript files to static
and the directory for saving images to media
.
mkdir static
mkdir media
In the created project directory, there is a file called settings.py
.
Add the following to this file.
settings.py
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
Created app name, #I will add here.
]
settings.py
import os
#Various codes (omitted)
STATIC_URL = '/static/'
#Add the following from here.
STATICFILES_DIRS = [os.path.join(BASE_DIR,Directory for storing CSS files)]
MEDIA_URL =Image URL
MEDIA_ROOT = os.path.join(BASE_DIR,Directory name to save the image)
LOGIN_URL =Login page URL
Add the above CSS file and image URL settings to ʻurls.py`.
urls.py
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
ʻIf settings.DEBUG` means only when running for development.
Create a template HTML file that describes the parts that are common to all HTML files.
I often create a templates
directory in the application directory, then a directory with the application name, and a file called base.html
in it.
In base.html
, write the storage location of the CSS file in the head
tag.
In my case
<link rel="stylesheet" href="{% static 'style.css' %}" type="text/css">
(Here, the CSS file name is style.css
).
base.html
<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="{%Directory name to save the CSS file CSS file name%}" type="text/css">
<title>Sample page</title>
</head>
<body>
</body>
</html>
And in other HTML files, write the following code to refer to base.html
.
sample.html
{%extends template HTML file name%}
With the directory structure described above, it will be {% extends'application name /base.html%}
.
Database-related settings are primarily described in models.py
, but the make migrations
command allows you to record changes to database-related settings.
If you omit the application name, it applies to all applications in the project.
python manage.py makemigrations (application name)
Then, if there are no problems such as errors, apply the changes to the database itself with the migrate
command.
You can also omit the application name here.
python manage.py migrate (application name)
To create a user with administrator privileges, enter the following command:
python manage.py createsuperuser
You will be asked for your user name, email address, and password, so enter them in order.
To start the local server (development server), execute the following command.
python manage.py runserver
You can also stop the server with Ctrl + C
.
Here's what you need to know to get started with Django. If you can understand these, you've taken the first step towards understanding Django.
Recommended Posts