Django defaults

Introduction

Here's what you need to know to get started with Django.

Create a Django project

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.

Creating an application

To create the application, run the following command.

python manage.py startapp application name

Creating a directory to store HTML files, CSS files, and images

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

Addition to settings.py

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

Addition to urls.py

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.

Creating an HTML template

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 settings

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)

Administrator settings

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.

Start local server

To start the local server (development server), execute the following command.

python manage.py runserver

You can also stop the server with Ctrl + C.

Summary

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

Django defaults
Django
django update
Django note 4
Django memorandum
django search
Django installation
Django Summary
Django test
Django # 2 (template)
Django Note 5
Django hands-on
Touch django
Django Summary
Django Shoho
Django + Docker
Django Glossary
Django search
Install Django
Django: References
Django Note 1
Django note 3
Django note 2
Django startup
Django notes
Django NullCharField
Django environment construction
Django ~ settings.py edition ~
Django Heroku Deploy 1
Django HTML Template # 2
Django Contact Form 2
Django begins part 1
Django model: ManyToManyField
What is Django? .. ..
Models in Django
Django function-based view
Python Django Tutorial (5)
Django Learning Memo
Python Django Tutorial (2)
[Django] as_view () notes
First Django Challenge
django makemigarations createsuperuser
Django related sites
Django version check
Django begins part 4
Django 1.9 for internationalization
CentOS8 --Play --Django
CentOS8 --Install --Django
django tutorial memo
[Django] Redo migrate
django environment construction
Python Django Tutorial (8)
Python Django Tutorial (6)
django default settings
Start Django Tutorial 1
Django HTML template
Django Template Tips
Django girls-3 workflow
Django Project Baseline
Django Heroku Deploy 2
CRUD with Django